项目目录
controller层
package com.yyzy.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yyzy.pojo.User;
import com.yyzy.service.UserService;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/login")
public String login(Model model) {
model.addAttribute("User", new User());
return "login";
}
@RequestMapping("/loginIn")
public String loginIn(Model model,User user,HttpServletRequest request,HttpServletResponse response) {
User u = userService.login(user);
if (u==null) {
//登录失败,返回登录页面
model.addAttribute("User", user);
model.addAttribute("errMsg", "用户名或密码错误");
return "login";
}
//查到了,跳主页
HttpSession session = request.getSession();
session.setAttribute("loginUser", u);
return "redirect:findAll.do";
}
@RequestMapping("/findAll")
public String mian(Model model,User user) {
List<User> list = userService.findAll(user);
model.addAttribute("list",list);
return "main";
}
@RequestMapping("/delete")
public String delete(Model model,int id) {
int num = userService.deleteId(id);
return "redirect:findAll.do";
}
@RequestMapping("/findById")
public String findById(Model model,int id) {
User user = userService.findById(id);
model.addAttribute("user",user);
return "update";
}
@RequestMapping("/updateById")
public String updateById(User user) {
int updateById = userService.updateById(user);
return "redirect:findAll.do";
}
@RequestMapping("/insert")
public String insert(User user) {
int add = userService.add(user);
return "redirect:findAll.do";
}
}
dao层
package com.yyzy.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.yyzy.pojo.User;
@Repository
public interface UserDao {
public User findByNamePwd(User user);
public List<User> findAll(User user);
public int deleteId(int id);
public User findById(int id);
public int updateById(User user);
public int add(User user);
}
userdao.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="com.yyzy.dao.UserDao">
<select id="findByNamePwd" resultType="User">
select * from t00_user where username=#{username} and password=#{password}
</select>
<select id="findAll" resultType="User">
select * from t00_user
<where>
<if test="username!=null and username!=''">
and username like concat("%",#{username},"%")
</if>
</where>
</select>
<delete id="deleteId">
delete from t00_user where id=#{id}
</delete>
<select id="findById" resultType="User">
select * from t00_user where id=#{id}
</select>
<update id="updateById">
update t00_user set username=#{username},realname=#{realname},password=#{password},flag=#{flag} where id=#{id}
</update>
<insert id="add">
insert into t00_user values(null,#{username},#{realname},#{password},#{flag})
</insert>
</mapper>
实体类
package com.yyzy.pojo;
import java.util.List;
public class PageBean<T> {
//传过来的参数
private int total;//总记录数
private int pageNum;//当前页
private int pageSize;//每页显示的条数
//可以算出来的数据
private int totalPage;//总页数
private int startIndex;//查询开始的索引
private List<T> list;//根据分页查询数据集
//5,6,7,8,9 start是5 end是9
private int start;
private int end;
public PageBean(int total, int pageNum, int pageSize) {
super();
this.total = total;
this.pageNum = pageNum;
this.pageSize = pageSize;
if (total%pageSize==0) {
this.totalPage=total/pageSize;
}else {
this.totalPage=total/pageSize+1;
}
}
}
package com.yyzy.pojo;
public class User {
private int id;
private String username;
private String realname;
private String password;
private String flag;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String username, String realname, String password, String flag) {
super();
this.id = id;
this.username = username;
this.realname = realname;
this.password = password;
this.flag = flag;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
}
service层
package com.yyzy.service;
import java.util.List;
import com.yyzy.pojo.User;
public interface UserService {
public User login(User user);
public List<User> findAll(User user);
public int deleteId(int id);
public User findById(int id);
public int updateById(User user);
public int add(User user);
}
service.impl
package com.yyzy.service.Impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yyzy.dao.UserDao;
import com.yyzy.pojo.User;
import com.yyzy.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserDao userdao;
@Override
public User login(User user) {
User u = userdao.findByNamePwd(user);
return u;
}
@Override
public List<User> findAll(User user) {
List<User> list = userdao.findAll(user);
return list;
}
@Override
public int deleteId(int id) {
int deleteId = userdao.deleteId(id);
return deleteId;
}
@Override
public User findById(int id) {
User findById = userdao.findById(id);
return findById;
}
@Override
public int updateById(User user) {
int updateById = userdao.updateById(user);
return updateById;
}
@Override
public int add(User user) {
int add = userdao.add(user);
return add;
}
}
配置文件
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.yyzy.pojo"/>
</typeAliases>
</configuration>
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 配置 读取properties文件 jdbc.properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
<!-- 设置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 设置Mapper扫描包 -->
<property name="basePackage" value="com.yyzy.dao" />
</bean>
</beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!-- 配置Service扫描 -->
<context:component-scan base-package="com.yyzy.service" />
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解方式管理AOP事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!-- 配置Service扫描 -->
<context:component-scan base-package="com.yyzy.service" />
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解方式管理AOP事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_coures
jdbc.username=root
jdbc.password=
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>SSM</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:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 配置监听器加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器 -->
<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:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 解决post请求乱码 -->
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>