web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>week14</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--配置spring文件的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--字符编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--配置数据库常量 -->
<context:property-placeholder
location="classpath:db.properties" />
<!--配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置开启事务注解驱动 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
<!--配置mybatis工厂 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--配置分页插件 -->
<property name="plugins">
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="dialect">mysql</prop><!--配置数据库类型 -->
<prop key="reasonable">true</prop><!--配置页码合理化修正 -->
</props>
</property>
</bean>
</property>
</bean>
<!--扫描mapper包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.test.mapper"></property>
</bean>
<!--扫描service包 -->
<context:component-scan
base-package="cn.test.service"></context:component-scan>
</beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/user
jdbc.username=root
jdbc.password=123456
springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--扫描控制层controller包 -->
<context:component-scan
base-package="cn.test.controller"></context:component-scan>
<!--开启mvc注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--前端控制器不拦截静态资源 -->
<mvc:default-servlet-handler />
</beans>
Controller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
//按uid查询
@RequestMapping("/queryUserByUid")
public String queryUser(int uid,Model model) {
User u=userService.queryUserByUid(uid);
model.addAttribute("user", u);
return "userinfo";
}
/*
* @RequestMapping("/getAllUser") public String getAllUser(Model model) {
* List<User> u=userService.getAllUser(); model.addAttribute("ulist", u); return
* "userinfo"; }
*/
//查询所有
@RequestMapping("/getAllUser")
public String getAllUser(Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize) {
PageHelper.startPage(pageNum, pageSize);//开始分页
List<User> u=userService.getAllUser();//查询所有用户信息
PageInfo<User> p=new PageInfo<User>(u);//封装了分页后的信息
model.addAttribute("ulist", u);
model.addAttribute("pageinfo", p);
model.addAttribute("flag", 1);
return "userinfo";
}
//按姓名查找
@RequestMapping("/queryUserByUname")
public String queryUserByUname(String uname,Model model,@RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "3")int pageSize) {
PageHelper.startPage(pageNum, pageSize);//开始分页
List<User> u=userService.queryUserByUname(uname);//查询所有用户信息
PageInfo<User> p=new PageInfo<User>(u);//封装了分页后的信息
model.addAttribute("ulist", u);
model.addAttribute("pageinfo", p);
model.addAttribute("flag", 0);
model.addAttribute("uname", uname);
return "userinfo";
}
//跳转到添加用户页面
@RequestMapping("/toAddUser")
public String toAddUser() {
return "addUser";
}
//添加用户
@RequestMapping("addUser")
public String addUser(User user) {
int t=userService.addUser(user);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
//删除用户
@RequestMapping("/deleteUser")
public String deleteUser(int uid) {
int t=userService.deleteUser(uid);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
//跳转到添加用户页面
@RequestMapping("/toUpdate")
public String toUpdate(int uid,Model model) {
model.addAttribute("uid", uid);
return "update";
}
//修改用户
@RequestMapping("/updateUser")
public String updateUser(User user) {
int t=userService.updateUser(user);
if(t>0) {
return "forward:getAllUser";
}else {
return "erro";
}
}
}
mapper
public interface UserMapper {
//根据用户编号查询用户信息,返回一个用户
public User queryUserByUid(int uid);//按uid查找
public List<User> getAllUser();//查询所有
public List<User> queryUserByUname(String uname);//按姓名查找
public int addUser(User user);//添加用户
public int deleteUser(int uid);//删除用户
public int updateUser(User user);//修改用户
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.test.mapper.UserMapper">
<select id="queryUserByUid" parameterType="Integer" resultType="cn.test.po.User">
select * from usertab where uid=#{_parameter}
</select>
<select id="getAllUser" resultType="cn.test.po.User">
select * from usertab
</select>
<select id="queryUserByUname" parameterType="String" resultType="cn.test.po.User">
select * from usertab where uname like concat('%',#{_parameter},'%')
</select>
<insert id="addUser" parameterType="cn.test.po.User">
insert into usertab(uname,age) values(#{uname},#{age})
</insert>
<delete id="deleteUser" parameterType="Integer">
delete from usertab where uid=#{_parameter}
</delete>
<update id="updateUser" parameterType="cn.test.po.User">
update usertab set uname=#{uname},age=#{age} where uid=#{uid}
</update>
</mapper>