我的中软国际线上学习日志05-SSM增删改查

    技术2022-07-11  76

    1.User.java

    package com.zr.bean; public class User { public User() { } public User(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public User(String username, String password) { this.username = username; this.password = password; } private int id; private String username; private String password; 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; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }

    2UserController.java

    package com.zr.controller; import com.zr.bean.User; import com.zr.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; @Controller @RequestMapping("/user") public class UserController { @Autowired private IUserService userService; @RequestMapping("/login.do") public ModelAndView login(User user){ boolean flag = userService.login(user.getUsername(), user.getPassword()); ModelAndView modelAndView=new ModelAndView(); if(flag){ modelAndView.setViewName("main"); }else{ modelAndView.setViewName("../failer"); } return modelAndView; } @RequestMapping("/findAll.do") public ModelAndView findAll(){ List<User> userList=userService.findAll(); ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("userList",userList); modelAndView.setViewName("user-list"); return modelAndView; } @RequestMapping("/deleteById.do") public String deleteById(int id){ userService.deleteById(id); return "redirect:findAll.do"; } @RequestMapping("/addUser.do") public String addUser(User user) { userService.addUser(user); return "redirect:findAll.do"; } @RequestMapping("/findOne.do") public ModelAndView findOne(int id) { User user=userService.findOne(id); ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("user",user); modelAndView.setViewName("user-update"); return modelAndView; } @RequestMapping("/update.do") public String updateById(User user) { userService.updateUser(user); return "redirect:findAll.do"; } }

    3.UserDao.java

    package com.zr.DAO; import com.zr.bean.User; import java.util.List; public interface UserDao { User findUserByUserName(String username); List<User> findAll(); void deleteById(int id); void addUser(User user); void updateUser(User user); User findOne(int id); }

    4.UserService.java

    package com.zr.service.impl; import com.zr.DAO.UserDao; import com.zr.bean.User; import com.zr.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService implements IUserService { @Autowired private UserDao userDao; @Override public boolean login(String username, String password) { User user=userDao.findUserByUserName(username); if (user!=null && user.getPassword().equals(password)){ return true; } return false; } @Override public List<User> findAll(){ return userDao.findAll(); } @Override public void deleteById(int id) { userDao.deleteById(id); } @Override public void addUser(User user) { userDao.addUser(user); } public void updateUser(User user) { userDao.updateUser(user); } public User findOne(int id) { return userDao.findOne(id); } }

    5.IUserService.java

    package com.zr.service; import com.zr.bean.User; import java.util.List; public interface IUserService { boolean login(String username, String password); List<User> findAll(); void deleteById(int id); public void addUser(User user); public void updateUser(User user); User findOne(int id); }

    6.UserMapping.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.zr.DAO.UserDao" > <select id="findUserByUserName" parameterType="String" resultType="com.zr.bean.User"> select * from tb_user where username=#{username} </select> <select id="findAll" parameterType="String" resultType="com.zr.bean.User"> select * from tb_user </select> <delete id="deleteById" parameterType="int" > delete from tb_user where id=#{id} </delete> <insert id="addUser" parameterType="user"> insert into tb_user(username,password) values(#{username},#{password}) </insert> <select id="findOne" parameterType="int" resultType="user"> select * from tb_user where id=#{id} </select> <update id="updateUser" parameterType="user" > update tb_user set username=#{username},password=#{password} where id=#{id} </update> </mapper>

    7.applicationContext.java

    <?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.配置数据库相关参数properties的属性:${url} --> <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="30"/> <property name="minPoolSize" value="2"/> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 扫描bean包 使用别名 --> <property name="typeAliasesPackage" value="com.zr.bean"></property> <!--配置加载映射文件 UserMapper.xml--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 自动生成dao,mapper--> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.zr.DAO"/> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--自动扫描--> <context:component-scan base-package="com.zr"/> <!-- 配置事务--> <!-- 5.配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 6.开启事务注解--> <tx:annotation-driven></tx:annotation-driven> </beans>

    8.db.proderties

    jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/wzbc?useSSL=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=123456

    9.spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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:component-scan base-package="com.zr.controller" /> <!-- 2.配置映射处理和适配器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 3.视图的解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
    Processed: 0.012, SQL: 9