1 环境准备

1.1 软件架构

JDK 1.8

Spring 4.x

Mybatis 3.x

Maven 3.x

MySQL 5.7

1.2 创建数据库

创建数据库,数据库名ssm-demo,字符集utf-8,排序规则默认。

  1. DROP TABLE IF EXISTS `t_user`;
  2. CREATE TABLE `t_user` (
  3. `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT,
  4. `username` VARCHAR ( 60 ) DEFAULT NULL,
  5. `password` VARCHAR ( 60 ) DEFAULT NULL,
  6. `nick_name` VARCHAR ( 60 ) DEFAULT NULL,
  7. `telephone` VARCHAR ( 60 ) DEFAULT NULL,
  8. `email` VARCHAR ( 60 ) DEFAULT NULL,
  9. `birthday` date DEFAULT NULL,
  10. `gender` CHAR ( 1 ) DEFAULT NULL,
  11. `status` CHAR ( 1 ) DEFAULT NULL,
  12. `create_time` datetime DEFAULT NULL,
  13. `update_time` datetime DEFAULT NULL,
  14. PRIMARY KEY ( `id` )
  15. );

1.3 Maven配置

修改settings.xml配置。

  1. <!-- 设置aliyun镜像 -->
  2. <mirrors>
  3. <mirror>
  4. <id>alimaven</id>
  5. <name>aliyun maven</name>
  6. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  7. <mirrorOf>central</mirrorOf>
  8. </mirror>
  9. </mirrors>
  10. <!-- 设置jdk1.8版本编译 -->
  11. <profiles>
  12. <profile>
  13. <id>jdk8</id>
  14. <activation>
  15. <activeByDefault>true</activeByDefault>
  16. <jdk>1.8</jdk>
  17. </activation>
  18. <properties>
  19. <maven.compiler.source>1.8</maven.compiler.source>
  20. <maven.compiler.target>1.8</maven.compiler.target>
  21. <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  22. </properties>
  23. </profile>
  24. </profiles>

1.4 创建工程

点击next,填写相关信息。

点击next创建项目。

在main文件夹下创建java文件夹、resources文件夹,在src文件夹下创建test文件夹,在test文件夹下创建java文件夹。

定义文件夹类型,main文件夹下java文件夹选择Sources Root,resources文件夹选择 Resources Root,test文件夹下java文件夹选择Tests。

点击ok。

1.4 配置tomcat

选择tomcat路径。

点击fix。

选择war-exploded。

修改Application context。

点击ok。

1.5 添加Maven依赖

完整的pom.xml。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.zl</groupId>
  6. <artifactId>ssm-demo</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>ssm-demo </name>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <maven.compiler.source>1.8</maven.compiler.source>
  13. <maven.compiler.target>1.8</maven.compiler.target>
  14. <spring.version>4.3.7.RELEASE</spring.version>
  15. </properties>
  16. <dependencies>
  17. <!-- spring、springmvc -->
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-webmvc</artifactId>
  21. <version>${spring.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-jdbc</artifactId>
  26. <version>${spring.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-test</artifactId>
  31. <version>${spring.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-aspects</artifactId>
  36. <version>${spring.version}</version>
  37. </dependency>
  38. <!-- mybatis -->
  39. <dependency>
  40. <groupId>org.mybatis</groupId>
  41. <artifactId>mybatis</artifactId>
  42. <version>3.4.2</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.mybatis</groupId>
  46. <artifactId>mybatis-spring</artifactId>
  47. <version>1.3.1</version>
  48. </dependency>
  49. <!-- pageHelper分页插件 -->
  50. <dependency>
  51. <groupId>com.github.pagehelper</groupId>
  52. <artifactId>pagehelper</artifactId>
  53. <version>5.0.0</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>mysql</groupId>
  57. <artifactId>mysql-connector-java</artifactId>
  58. <version>5.1.41</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>c3p0</groupId>
  62. <artifactId>c3p0</artifactId>
  63. <version>0.9.1</version>
  64. </dependency>
  65. <!-- jstl、servlet-api -->
  66. <dependency>
  67. <groupId>jstl</groupId>
  68. <artifactId>jstl</artifactId>
  69. <version>1.2</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>javax.servlet</groupId>
  73. <artifactId>javax.servlet-api</artifactId>
  74. <version>3.0.1</version>
  75. <scope>provided</scope>
  76. </dependency>
  77. <!-- junit -->
  78. <dependency>
  79. <groupId>junit</groupId>
  80. <artifactId>junit</artifactId>
  81. <version>4.12</version>
  82. </dependency>
  83. </dependencies>
  84. <build>
  85. <finalName>ssm-demo</finalName>
  86. <pluginManagement>
  87. <plugins>
  88. <plugin>
  89. <artifactId>maven-clean-plugin</artifactId>
  90. <version>3.1.0</version>
  91. </plugin>
  92. <plugin>
  93. <artifactId>maven-resources-plugin</artifactId>
  94. <version>3.0.2</version>
  95. </plugin>
  96. <plugin>
  97. <artifactId>maven-compiler-plugin</artifactId>
  98. <version>3.8.0</version>
  99. </plugin>
  100. <plugin>
  101. <artifactId>maven-surefire-plugin</artifactId>
  102. <version>2.22.1</version>
  103. </plugin>
  104. <plugin>
  105. <artifactId>maven-war-plugin</artifactId>
  106. <version>3.2.2</version>
  107. </plugin>
  108. <plugin>
  109. <artifactId>maven-install-plugin</artifactId>
  110. <version>2.5.2</version>
  111. </plugin>
  112. <plugin>
  113. <artifactId>maven-deploy-plugin</artifactId>
  114. <version>2.8.2</version>
  115. </plugin>
  116. </plugins>
  117. </pluginManagement>
  118. </build>
  119. </project>

1.6 添加配置文件

1.6.1 applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  15. <!-- 配置包扫描 -->
  16. <context:component-scan base-package="com.zl" />
  17. <!-- 引入配置文件 -->
  18. <context:property-placeholder location="classpath:jdbc.properties"/>
  19. <!-- 配置数据源 -->
  20. <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  21. <property name="driverClass" value="${jdbc.driverClass}"/>
  22. <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
  23. <property name="user" value="${jdbc.user}"/>
  24. <property name="password" value="${jdbc.password}"/>
  25. </bean>
  26. <!-- Spring整合Mybatis -->
  27. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  28. <!-- 指定Mybatis核心配置文件 -->
  29. <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
  30. <!-- 配置数据源 -->
  31. <property name="dataSource" ref="myDataSource"/>
  32. <!-- 指定Mapper文件的位置 -->
  33. <property name="mapperLocations" value="classpath:mapper/*.xml"/>
  34. </bean>
  35. <!-- Mapper代理形式开发dao,扫描包形式配置mapper -->
  36. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  37. <property name="basePackage" value="com.zl.dao"/>
  38. </bean>
  39. <!-- 配置事务管理器 -->
  40. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  41. <property name="dataSource" ref="myDataSource"/>
  42. </bean>
  43. <!-- 通知 -->
  44. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  45. <tx:attributes>
  46. <!-- 传播行为 -->
  47. <tx:method name="save*" propagation="REQUIRED" />
  48. <tx:method name="insert*" propagation="REQUIRED" />
  49. <tx:method name="add*" propagation="REQUIRED" />
  50. <tx:method name="create*" propagation="REQUIRED" />
  51. <tx:method name="delete*" propagation="REQUIRED" />
  52. <tx:method name="update*" propagation="REQUIRED" />
  53. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  54. <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
  55. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  56. <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
  57. </tx:attributes>
  58. </tx:advice>
  59. <!-- 切面 -->
  60. <aop:config>
  61. <!-- 切入点表达式 -->
  62. <aop:pointcut expression="execution(* com.zl.service..*(..))" id="txPoint"/>
  63. <!-- 配置事务增强 -->
  64. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
  65. </aop:config>
  66. </beans>

1.6.2 springMvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  12. <!-- 配置Controller包扫描 -->
  13. <context:component-scan base-package="com.zl.controller"/>
  14. <!-- 配置注解驱动 -->
  15. <mvc:annotation-driven/>
  16. <!-- 配置视图解析器 -->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <!-- 前缀 -->
  19. <property name="prefix" value="/WEB-INF/jsp/"/>
  20. <!-- 后缀 -->
  21. <property name="suffix" value=".jsp"/>
  22. </bean>
  23. </beans>

1.6.3 sqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <settings>
  7. <setting name="mapUnderscoreToCamelCase" value="true"/>
  8. </settings>
  9. <typeAliases>
  10. <!-- 批量别名定义,扫描整个包下的类,别名为类名(大小写不敏感) -->
  11. <package name="com.zl.domain" />
  12. </typeAliases>
  13. <plugins>
  14. <!-- pageHelper分页插件配置 -->
  15. <plugin interceptor="com.github.pagehelper.PageInterceptor">
  16. <!-- 分页参数合理化 -->
  17. <property name="reasonable" value="true"/>
  18. </plugin>
  19. </plugins>
  20. </configuration>

1.6.4 jdbc.properties

  1. jdbc.driverClass=com.mysql.jdbc.Driver
  2. jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm-demo
  3. jdbc.user=root
  4. jdbc.password=1234

1.6.5 log4j.properties

  1. log4j.rootLogger=DEBUG, stdout
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

1.6.6 web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <welcome-file-list>
  7. <welcome-file>index.jsp</welcome-file>
  8. </welcome-file-list>
  9. <!-- Spring上下文的位置 -->
  10. <context-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath:applicationContext.xml</param-value>
  13. </context-param>
  14. <!-- Spring的监听器 -->
  15. <listener>
  16. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  17. </listener>
  18. <!-- SpringMVC前端控制器 -->
  19. <servlet>
  20. <servlet-name>dispatcherServlet</servlet-name>
  21. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22. <init-param>
  23. <param-name>contextConfigLocation</param-name>
  24. <!-- 此处不配置 默认找 /WEB-INF/[servlet-name]-servlet.xml -->
  25. <param-value>classpath:springMvc.xml</param-value>
  26. </init-param>
  27. <load-on-startup>1</load-on-startup>
  28. </servlet>
  29. <servlet-mapping>
  30. <servlet-name>dispatcherServlet</servlet-name>
  31. <url-pattern>/</url-pattern>
  32. </servlet-mapping>
  33. <!-- 字符编码过滤器,一定要放在所有过滤器之前 -->
  34. <filter>
  35. <filter-name>CharacterEncodingFilter</filter-name>
  36. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  37. <init-param>
  38. <param-name>encoding</param-name>
  39. <param-value>utf-8</param-value>
  40. </init-param>
  41. <init-param>
  42. <param-name>forceRequestEncoding</param-name>
  43. <param-value>true</param-value>
  44. </init-param>
  45. <init-param>
  46. <param-name>forceResponseEncoding</param-name>
  47. <param-value>true</param-value>
  48. </init-param>
  49. </filter>
  50. <filter-mapping>
  51. <filter-name>CharacterEncodingFilter</filter-name>
  52. <url-pattern>/*</url-pattern>
  53. </filter-mapping>
  54. </web-app>

1.7 编写代码

1.7.1 domain代码

  1. @Data
  2. public class User implements Serializable {
  3. private static final long serialVersionUID = -7761320977621867375L;
  4. private Long id;
  5. private String username;
  6. private String password;
  7. private String nickName;
  8. private String telephone;
  9. private String email;
  10. private Date birthday;
  11. private String gender;
  12. private String status;
  13. private Date createTime;
  14. private Date updateTime;
  15. }

1.7.2 dao代码

  1. public interface UserDao {
  2. List<User> queryUsersList(User user);
  3. int queryUsersCount(User user);
  4. User queryUserById(Long id);
  5. List<User> queryUsers();
  6. List<User> queryUsersByIds(Long[] ids);
  7. void insertUser(User user);
  8. void updateUser(User user);
  9. void deleteUserById(Long id);
  10. void deleteUsersByIds(Long[] ids);
  11. }

1.7.3 mapper文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.zl.dao.UserDao">
  6. <!-- 声明sql片段 -->
  7. <sql id="userColumns">
  8. id
  9. , username, password, nick_name, telephone, email, birthday, gender, status, create_time, update_time
  10. </sql>
  11. <!-- resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询 -->
  12. <resultMap id="userResult" type="User">
  13. <id property="id" column="id"/>
  14. <result property="username" column="username"/>
  15. <result property="password" column="password"/>
  16. <result property="nickName" column="nick_name"/>
  17. <result property="telephone" column="telephone"/>
  18. <result property="email" column="email"/>
  19. <result property="birthday" column="birthday"/>
  20. <result property="gender" column="gender"/>
  21. <result property="status" column="status"/>
  22. <result property="createTime" column="create_time"/>
  23. <result property="updateTime" column="update_time"/>
  24. <!-- association :配置一对一的关系 -->
  25. <!-- collection :配置一对多的关系 -->
  26. </resultMap>
  27. <!-- 分页查询 -->
  28. <!-- 注意这里的 resultType 返回值类型是集合内存储数据的类型,不是 'list' -->
  29. <select id="queryUsersList" parameterType="User"
  30. resultMap="userResult">
  31. select
  32. <include refid="userColumns"/>
  33. from t_user
  34. <where>
  35. <if test="username != null and username != ''">
  36. and username like concat('%', #{username}, '%')
  37. </if>
  38. <if test="nickName != null and nickName != ''">
  39. and nick_name like concat('%', #{nickName}, '%')
  40. </if>
  41. <if test="telephone != null and telephone != ''">
  42. and telephone like concat('%', #{telephone}, '%')
  43. </if>
  44. </where>
  45. </select>
  46. <!-- 查询条数 -->
  47. <select id="queryUsersCount" parameterType="User"
  48. resultType="Integer">
  49. select count(1) from t_user
  50. <where>
  51. <if test="username != null and username != ''">
  52. and username like concat('%', #{username}, '%')
  53. </if>
  54. <if test="nickName != null and nickName != ''">
  55. and nick_name like concat('%', #{nickName}, '%')
  56. </if>
  57. <if test="telephone != null and telephone != ''">
  58. and telephone like concat('%', #{telephone}, '%')
  59. </if>
  60. </where>
  61. </select>
  62. <!-- 查询全部 -->
  63. <select id="queryUsers" parameterType="String" resultMap="userResult">
  64. select
  65. <include refid="userColumns"/>
  66. from t_user
  67. </select>
  68. <!-- 精确查询 -->
  69. <select id="queryUserById" parameterType="Long" resultMap="userResult">
  70. select
  71. <include refid="userColumns"/>
  72. from
  73. t_user where id = #{id}
  74. </select>
  75. <!-- 批量查询 -->
  76. <select id="queryUsersByIds" parameterType="Long" resultMap="userResult">
  77. select
  78. <include refid="userColumns"/>
  79. from
  80. t_user
  81. <where>
  82. <foreach collection="array" item="id" open="id in (" close=")"
  83. separator=",">
  84. #{id}
  85. </foreach>
  86. </where>
  87. </select>
  88. <!-- 添加 -->
  89. <insert id="insertUser" parameterType="User">
  90. <!-- mysql自增主键返回,数据库表主键设置为自动递增 -->
  91. <selectKey keyProperty="id" keyColumn="id" order="AFTER" resultType="Long">
  92. select last_insert_id()
  93. </selectKey>
  94. insert into t_user
  95. (username, password, nick_name, telephone, email, birthday, gender, status, create_time, update_time)
  96. values (#{username}, #{password}, #{nickName}, #{email}, #{telephone}, #{birthday}, #{gender},
  97. #{status}, #{createTime},
  98. #{updateTime})
  99. </insert>
  100. <!-- 更新 -->
  101. <update id="updateUser" parameterType="User">
  102. update t_user
  103. <set>
  104. <if test="username != null and username != ''">
  105. username = #{username},
  106. </if>
  107. <if test="password != null and password != ''">
  108. password = #{password},
  109. </if>
  110. <if test="nickName != null and nickName != ''">
  111. nick_name = #{nickName},
  112. </if>
  113. <if test="telephone != null and telephone != ''">
  114. telephone = #{telephone},
  115. </if>
  116. <if test="email != null and email != ''">
  117. email = #{email},
  118. </if>
  119. <if test="birthday != null">
  120. birthday = #{birthday},
  121. </if>
  122. <if test="gender != null and gender != ''">
  123. gender = #{gender},
  124. </if>
  125. <if test="status != null and status != ''">
  126. status = #{status},
  127. </if>
  128. <if test="createTime != null">
  129. create_time = #{createTime},
  130. </if>
  131. <if test="updateTime != null">
  132. update_time = #{updateTime},
  133. </if>
  134. </set>
  135. where id = #{id}
  136. </update>
  137. <!-- 删除 -->
  138. <delete id="deleteUserById" parameterType="Long">
  139. delete
  140. from t_user
  141. where id = #{id}
  142. </delete>
  143. <!-- 批量删除 -->
  144. <delete id="deleteUsersByIds" parameterType="Long">
  145. delete from t_user
  146. <where>
  147. <!-- foreach标签,进行遍历 -->
  148. <!-- collection:遍历的集合-->
  149. <!-- item:遍历的项目,可以随便写,,但是和后面的#{}里面要一致 -->
  150. <!-- open:在前面添加的sql片段 -->
  151. <!-- close:在结尾处添加的sql片段 -->
  152. <!-- separator:指定遍历的元素之间使用的分隔符 -->
  153. <foreach collection="array" item="id" open="id in (" close=")"
  154. separator=",">
  155. #{id}
  156. </foreach>
  157. </where>
  158. </delete>
  159. <!-- 根据用户名密码查询用户 -->
  160. <select id="queryUserByUsernameAndPassword" parameterType="User" resultMap="userResult">
  161. select
  162. <include refid="userColumns"/>
  163. from
  164. t_user
  165. <where>
  166. <if test="username != null and username != ''">
  167. and username = #{username}
  168. </if>
  169. <if test="password != null and password != ''">
  170. and password = #{password}
  171. </if>
  172. </where>
  173. </select>
  174. </mapper>

1.7.4 service代码

  1. public interface UserService {
  2. List<User> queryUsersList(User user);
  3. int queryUsersCount(User user);
  4. User queryUserById(Long id);
  5. List<User> queryUsers();
  6. List<User> queryUsersByIds(Long[] ids);
  7. void insertUser(User user);
  8. void updateUser(User user);
  9. void deleteUserById(Long id);
  10. void deleteUsersByIds(Long[] ids);
  11. }
  1. @Service
  2. public class UserServiceImpl implements UserService {
  3. @Autowired
  4. private UserDao userDao;
  5. @Override
  6. public List<User> queryUsersList(User user) {
  7. return userDao.queryUsersList(user);
  8. }
  9. @Override
  10. public int queryUsersCount(User user) {
  11. return userDao.queryUsersCount(user);
  12. }
  13. @Override
  14. public User queryUserById(Long id) {
  15. return userDao.queryUserById(id);
  16. }
  17. @Override
  18. public List<User> queryUsers() {
  19. return userDao.queryUsers();
  20. }
  21. @Override
  22. public List<User> queryUsersByIds(Long[] ids) {
  23. return userDao.queryUsersByIds(ids);
  24. }
  25. @Override
  26. public void insertUser(User user) {
  27. userDao.insertUser(user);
  28. }
  29. @Override
  30. public void updateUser(User user) {
  31. userDao.updateUser(user);
  32. }
  33. @Override
  34. public void deleteUserById(Long id) {
  35. userDao.deleteUserById(id);
  36. }
  37. @Override
  38. public void deleteUsersByIds(Long[] ids) {
  39. userDao.deleteUsersByIds(ids);
  40. }
  41. @Override
  42. public User queryUserByUsernameAndPassword(User user) {
  43. return userDao.queryUserByUsernameAndPassword(user);
  44. }
  45. }

1.7.5 controller代码

1.8 测试

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @WebAppConfiguration
  3. @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
  4. public class UserTest {
  5. @Autowired
  6. private UserDao userDao;
  7. @Test
  8. public void insert() {
  9. for (int i = 0; i < 20; i++) {
  10. User user = new User();
  11. user.setUsername("user" + i);
  12. user.setPassword("123456");
  13. user.setNickName("用户" + i);
  14. user.setCreateTime(new Date());
  15. userDao.insertUser(user);
  16. }
  17. }
  18. @Test
  19. public void update() {
  20. User user = userDao.queryUserById(2L);
  21. user.setUsername("hahaha");
  22. user.setUpdateTime(new Date());
  23. userDao.updateUser(user);
  24. }
  25. @Test
  26. public void deleteById() {
  27. userDao.deleteUserById(2L);
  28. }
  29. @Test
  30. public void deleteByIds() {
  31. userDao.deleteUsersByIds(new Long[]{3L, 4L});
  32. }
  33. @Test
  34. public void query() {
  35. // pageHelper分页的一种
  36. PageHelper.startPage(1, 10);
  37. PageHelper.orderBy("create_time desc");
  38. List<User> list = userDao.queryUsersList(new User());
  39. int count = userDao.queryUsersCount(new User());
  40. System.out.println(list);
  41. System.out.println(count);
  42. }
  43. }

SpringMVC、Spring、MyBatis整合(IDEA版)的更多相关文章

  1. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  2. SpringMVC+Spring+Mybatis整合

    SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...

  3. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

  4. 2.springMVC+spring+Mybatis整合

    前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...

  5. ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)

    在springmvc+spring+mybatis里整合ztree实现增删改查,上一篇已经写了demo,以下就仅仅贴出各层实现功能的代码: Jsp页面实现功能的js代码例如以下: <script ...

  6. mybatis学习(十一)——springmvc++spring+mybatis整合

    做任何一个项目都以一个需求,这里先定义一下需求:利用三大框架查询酒店列表. 一.搭建开发环境 1.创建一个web项目 我这里用的是 jdk1.8+tomact7.0 2.创建hotel表 CREATE ...

  7. JavaWeb_(SpringMVC框架)SpringMVC&Spring&MyBatis整合

    JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合 传送门 1.整合ssm 3大框架 过程 a)导包 -> spring_Ja ...

  8. SpringMVC Spring Mybatis整合篇

    1.创建WEB项目 创建项目:(ssmbuild)步骤略........ 给项目添加lib文件夹,用于存放jar包: 在WEB-INF目录下创建lib文件夹: 创建完成:运行项目时需要把jar导入到l ...

  9. springMVC + Spring + MyBatis 整合

    整理下SSM(基于注解)的整合 1. web.xml 配置文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  10. SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置

    一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不 ...

随机推荐

  1. python opencv cv2 imshow threading 多线程

    除了线程同步,还需要注意的是「窗口处理」要放在主线程 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import threadin ...

  2. Tensorflow之TFRecord的原理和使用心得

    本文首发于微信公众号「对白的算法屋」 大家好,我是对白. 目前,越来越多的互联网公司内部都有自己的一套框架去训练模型,而模型训练时需要的数据则都保存在分布式文件系统(HDFS)上.Hive作为构建在H ...

  3. 解决win10 cmd运行python弹出windows应用商店下python应用程序

    方法一: 1.我一开始下载完python后,忘记下载到哪个位置,在win10底下输入框搜索python,点击打开文件所在位置,所在位置是python快捷键的位置,直接复制进行环境配置 配置完环境变量后 ...

  4. Python - pip-review 库

    使用 pip-review 库(推荐) 安装库 pip install pip-review 检查是否有需要更新的包 > pip-review scikit-learn==0.23.2 is a ...

  5. npm 设置同时从多个包源加载包的方法

    随着前后端分离技术的发展成熟,越来越来越多的后台系统甚至前端系统采用前后端分离方式,在大型前后端分离系统中,前端往往包含大量的第三方js 包的引用,各个第三方包又可能依赖另外一个第三方包,因此急需要一 ...

  6. python库--pandas--Series.str--字符串处理

    原数据 import pandas as pd a = pd.Series(['aSd', 'asd', 'dfd fsAsf sfs']) b = pd.Series([None, 'asd', ' ...

  7. python库--tensorflow--数学函数

    官方API(需FQ) 中文API 方法 返回值类型 参数 说明 算数运算符 .add() Tensor x, y, name=N 加法(若x,y都为tensor, 数据类型需一致, 以下所有x,y都如 ...

  8. python基础--网站推荐

    Python教程 - 廖雪峰的官方网站 Python 基础教程 | 菜鸟教程 随笔分类 - 机器学习

  9. reeswitch http https ws wss nginx domain default port config

    现代H5浏览器产业链越来越丰富,http+websocket+webrtc+sip组合已经是一种非常成熟的web原生音视频通讯解决方案 FreeSWITCH是一个开源的电话软交换平台,早在SIP年代就 ...

  10. Excel表格中单击一个单元格如何将整行整列变色

    视图->阅读模式 开启阅读模式后 就会显示如下情景,是的你点击任意单元格后,显示整行/整列