1.实体类(UserInfo)
package com.zjitc.bean;
/**
* UserInfo - 实体类,映射数据库字段,字段建议一致
*/
public class UserInfo {
private int id; //用户ID
private String username; //用户名
private String password; //用户密码
// get/set 访问器
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// 构造方法
public UserInfo() {
}
public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
// toString的方法重写
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.dao层(IUserInfoDao)
package com.zjitc.dao;
import com.zjitc.bean.UserInfo;
import java.util.List;
public interface IUserInfoDao {
// 验证登陆
UserInfo doLogin(String username);
// 全部查询
List<UserInfo> findAll(Integer page,Integer size);
// 添加用户
void addUser(UserInfo userInfo);
// 删除用户
void delUserById(int id);
// 修改前查询
UserInfo updSelUserInfoById(int id);
// 修改用户
void updUserById(UserInfo userInfo);
}
3.Service层
(1) 接口类(IUserInfoService)
package com.zjitc.service;
import com.zjitc.bean.UserInfo;
import java.util.List;
public interface IUserInfoService {
UserInfo doLogin(String username);
List<UserInfo> findAll(Integer page,Integer size);
void addUser(UserInfo userInfo);
void delUserById(int id);
UserInfo updSelUserInfoById(int id);
void updUserById(UserInfo userInfo);
}
(2) 实现类(UserInfoServiceImpl)
package com.zjitc.service.impl;
import com.github.pagehelper.PageHelper;
import com.zjitc.bean.UserInfo;
import com.zjitc.dao.IUserInfoDao;
import com.zjitc.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserInfoServiceImpl implements IUserInfoService {
// @Resource(name = "u1")
@Autowired
private IUserInfoDao userInfoDao;
@Override
public UserInfo doLogin(String username) {
return userInfoDao.doLogin(username);
}
@Override
public List<UserInfo> findAll(Integer page,Integer size) {
PageHelper.startPage(page,size);
return userInfoDao.findAll(page,size);
}
@Override
public void addUser(UserInfo userInfo) {
userInfoDao.addUser(userInfo);
}
@Override
public void delUserById(int id) {
userInfoDao.delUserById(id);
}
@Override
public UserInfo updSelUserInfoById(int id) {
return userInfoDao.updSelUserInfoById(id);
}
@Override
public void updUserById(UserInfo userInfo) {
userInfoDao.updUserById(userInfo);
}
}
4.Controller层(UserInfoController)
package com.zjitc.controller;
import com.github.pagehelper.PageInfo;
import com.zjitc.bean.UserInfo;
import com.zjitc.service.IUserInfoService;
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 org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserInfoController {
@Autowired
private IUserInfoService userInfoService;
@RequestMapping("/doLogin.do")
public String doLogin(String username, String password, HttpSession session){
// 1、调用service,查询得到结果,自动补全返回值 ctrl+alt+v
UserInfo userInfo = userInfoService.doLogin(username);
// 判断
if(userInfo != null){
if (userInfo.getPassword().equals(password)){
System.out.println("密码正确,登陆成功");
session.setAttribute("userInfo",userInfo);
return "main";
}
else {
System.out.println("密码错误,登陆失败");
session.setAttribute("message","密码错误,登陆失败");
return "../login";
}
}
else {
System.out.println("用户不存在,请重新输入");
session.setAttribute("message","用户不存在,请重新输入");
return "../login";
}
}
// 全部查询
// 所有需要返回数据到页面显示的全部都需要封装到ModelAndView中
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "5") Integer size){
List<UserInfo> userInfoList = userInfoService.findAll(page,size);
PageInfo pageInfo = new PageInfo(userInfoList);
ModelAndView mv = new ModelAndView();
mv.addObject("pageInfo",pageInfo);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("/addUser.do")
public String addUser(UserInfo userInfo){
userInfoService.addUser(userInfo);
return "redirect:findAll.do";
}
@RequestMapping("/delUser.do")
public String delUserById(int id){
userInfoService.delUserById(id);
System.out.println("删除成功");
return "redirect:findAll.do";
}
@RequestMapping("/updSelUserById.do")
public ModelAndView updSelUserInfoById(int id){
UserInfo userInfo = userInfoService.updSelUserInfoById(id);
ModelAndView mv = new ModelAndView();
mv.addObject("userInfo",userInfo);
mv.setViewName("user-update");
return mv;
}
@RequestMapping("/updUser.do")
public String updUserById(UserInfo userInfo){
userInfoService.updUserById(userInfo);
System.out.println("更新成功");
return "redirect:findAll.do";
}
}
5.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: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.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 1、配置数据库相关参数-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2、配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- <property name="maxPoolSize" value="20"/>-->
<!-- <property name="minPoolSize" value="3"/>-->
</bean>
<!-- 3、配置sqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描dao层 使用别名-->
<property name="typeAliasesPackage" value="com.zjitc.bean"/>
<!-- 配合加载映射文件 *mapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 分页插件pageHelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!-- 自动生成dao,mapper-->
<!-- 4、配置扫描dao接口包,将dao层注入spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置给出需要扫描的dao层路径-->
<property name="basePackage" value="com.zjitc.dao"/>
<!-- 注入Spring容器中-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 自动扫描全部包,注解支持-->
<context:component-scan base-package="com.zjitc"/>
<!-- 配置事务注解-->
<bean id="transactionMapper" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven></tx:annotation-driven>
</beans>
6.mapper文件(UserInfoMapper.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.zjitc.dao.IUserInfoDao">
<!-- 登陆-->
<select id="doLogin" parameterType="String" resultType="com.zjitc.bean.UserInfo">
select * from tb_user where username=#{username}
</select>
<!-- 全部查询-->
<select id="findAll" resultType="com.zjitc.bean.UserInfo">
select * from tb_user
</select>
<insert id="addUser" parameterType="com.zjitc.bean.UserInfo">
insert into tb_user(id,username,password) values (#{id},#{username},#{password})
</insert>
<delete id="delUserById" parameterType="Integer">
delete from tb_user where id=#{id}
</delete>
<select id="updSelUserInfoById" parameterType="Integer" resultType="com.zjitc.bean.UserInfo">
select * from tb_user where id=#{id}
</select>
<update id="updUserById" parameterType="com.zjitc.bean.UserInfo">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>
来源:CSDN
作者:IceDream556
链接:https://blog.csdn.net/IceDream556/article/details/104608891