一、导入依赖

  1. <!--依赖-->
  2. <dependencies>
  3. <!-- pagehelper:分页插件 -->
  4. <dependency>
  5. <groupId>com.github.pagehelper</groupId>
  6. <artifactId>pagehelper</artifactId>
  7. <version>5.1.10</version>
  8. </dependency>
  9. <!--Junit-->
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>4.12</version>
  14. </dependency>
  15. <!--mysql jdbc数据库驱动-->
  16. <dependency>
  17. <groupId>mysql</groupId>
  18. <artifactId>mysql-connector-java</artifactId>
  19. <version>5.1.47</version>
  20. </dependency>
  21. <!-- 数据库连接池 -->
  22. <dependency>
  23. <groupId>com.alibaba</groupId>
  24. <artifactId>druid</artifactId>
  25. <version>1.1.16</version>
  26. </dependency>
  27. <!--Servlet支持-->
  28. <dependency>
  29. <groupId>javax.servlet</groupId>
  30. <artifactId>servlet-api</artifactId>
  31. <version>2.5</version>
  32. </dependency>
  33. <!--jsp-api-->
  34. <dependency>
  35. <groupId>javax.servlet.jsp</groupId>
  36. <artifactId>jsp-api</artifactId>
  37. <version>2.2</version>
  38. </dependency>
  39. <!--添加jstl支持-->
  40. <dependency>
  41. <groupId>javax.servlet</groupId>
  42. <artifactId>jstl</artifactId>
  43. <version>1.1.2</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>taglibs</groupId>
  47. <artifactId>standard</artifactId>
  48. <version>1.1.2</version>
  49. </dependency>
  50. <!--Spring 支持-->
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-core</artifactId>
  54. <version>5.2.12.RELEASE</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework</groupId>
  58. <artifactId>spring-beans</artifactId>
  59. <version>5.2.12.RELEASE</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring-context</artifactId>
  64. <version>5.2.12.RELEASE</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-context-support</artifactId>
  69. <version>5.2.12.RELEASE</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-web</artifactId>
  74. <version>5.2.12.RELEASE</version>
  75. </dependency>
  76. <!--Spring mcv支持-->
  77. <dependency>
  78. <groupId>org.springframework</groupId>
  79. <artifactId>spring-webmvc</artifactId>
  80. <version>5.1.9.RELEASE</version>
  81. </dependency>
  82. <!--Mybatis-->
  83. <dependency>
  84. <groupId>org.mybatis</groupId>
  85. <artifactId>mybatis</artifactId>
  86. <version>3.4.5</version>
  87. </dependency>
  88. <dependency>
  89. <groupId>org.mybatis</groupId>
  90. <artifactId>mybatis-spring</artifactId>
  91. <version>1.3.2</version>
  92. </dependency>
  93. <!--spring jdbc支持-->
  94. <dependency>
  95. <groupId>org.springframework</groupId>
  96. <artifactId>spring-jdbc</artifactId>
  97. <version>5.1.9.RELEASE</version>
  98. </dependency>
  99. <dependency>
  100. <groupId>org.projectlombok</groupId>
  101. <artifactId>lombok</artifactId>
  102. <version>1.18.24</version>
  103. </dependency>
  104. <!--spring aop编程支持-->
  105. <dependency>
  106. <groupId>org.aspectj</groupId>
  107. <artifactId>aspectjweaver</artifactId>
  108. <version>1.6.8</version>
  109. </dependency>
  110. <dependency>
  111. <groupId>org.springframework</groupId>
  112. <artifactId>spring-aop</artifactId>
  113. <version>5.2.12.RELEASE</version>
  114. </dependency>
  115. </dependencies>
  116. <!--资源为加载进去,加入下列代码-->
  117. <build>
  118. <resources>
  119. <resource>
  120. <directory>src/main/resources</directory>
  121. <includes>
  122. <!--按需求添加-->
  123. <include>**/*.xml</include>
  124. <include>**/*.properties</include>
  125. </includes>
  126. <filtering>true</filtering>
  127. </resource>
  128. <resource>
  129. <directory>src/main/java</directory>
  130. <includes>
  131. <!--按需求添加-->
  132. <include>**/*.xml</include>
  133. <include>**/*.properties</include>
  134. </includes>
  135. <filtering>true</filtering>
  136. </resource>
  137. </resources>
  138. </build>

二、编写逻辑代码

1、编写实体类Admin

2、编写映射接口mapper文件夹(也可以是dao文件夹,看个人习惯)

  1. public interface AdminMapper {
  2. // 根据id查询用户
  3. Admin selectById(Integer id);
  4. }

3、编写AdminMapper.xml

注意:

  1. 方式一:可以放在资源目录下(一般把xml文件放在mapper),然后编写映射配置
  2. 方式二:我这里在mapper文件夹下创建xml,用来存放xml文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.shuai.mapper.AdminMapper">
  5. <select id="selectById" parameterType="java.lang.Integer" resultType="Admin">
  6. select * from admin where id = #{id,jdbcType=INTEGER}
  7. </select>
  8. </mapper>

强调:扫描的接口一定要写对,不然调试会找不到,这是个大坑,单词一定要看清楚

4、编写service层(AdminService)去调dao层

  1. public interface AdminService {
  2. // 根据id查询
  3. Admin queryAdminById(Integer id);
  4. }

5、编写service层的实现类(我放在service文件夹下的impl下)AdminServiceImpl

  1. @Service("adminService") // 注入到bean中
  2. public class AdminServiceImpl implements AdminService {
  3. @Autowired
  4. private AdminMapper adminMapper;
  5. public Admin queryAdminById(Integer id) {
  6. return adminMapper.selectById(id);
  7. }
  8. }

6、最后,通过Controller层调用返回给页面(AdminController)

  1. @Controller
  2. public class AdminController {
  3. @Autowired
  4. private AdminService adminService;
  5. @GetMapping("/admin")
  6. public String list(Model model){
  7. Admin admin = adminService.queryAdminById(1);
  8. model.addAttribute("list",admin);
  9. return "query";
  10. }
  11. }

三、核心配置

1、db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/library_system?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
  3. jdbc.username=root
  4. jdbc.password=shuai

2、mybatis-config.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. <!-- 全局配置 -->
  7. <settings>
  8. <!--允许 JDBC 支持自动生成主键-->
  9. <setting name="useGeneratedKeys" value="false"/>
  10. <!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典
  11. Java 属性名 aColumn 的类似映射。 -->
  12. <setting name="mapUnderscoreToCamelCase" value="true"/>
  13. <!-- 打印查询语句 -->
  14. <!-- <setting name="logImpl" value="STDOUT_LOGGING" />-->
  15. </settings>
  16. <plugins>
  17. <plugin interceptor="com.github.pagehelper.PageInterceptor">
  18. <!--<property name="dialect" value="mysql"/>-->
  19. <!--<property name="offsetAsPageNum" value="false"/>-->
  20. <!--<property name="rowBoundsWithCount" value="false"/>-->
  21. <!--<property name="pageSizeZero" value="true"/>-->
  22. <property name="reasonable" value="true"/>
  23. <!--<property name="supportMethodsArguments" value="false"/>-->
  24. <!--<property name="returnPageInfo" value="none"/>-->
  25. </plugin>
  26. </plugins>
  27. </configuration>

3、spring-mybatis.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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--开启注解支持-->
  10. <context:annotation-config/>
  11. <!--关联数据库文件-->
  12. <context:property-placeholder location="classpath:db.properties"/>
  13. <!--配置 Druid连接池-->
  14. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  15. <property name="driverClassName" value="${jdbc.driver}"/>
  16. <property name="url" value="${jdbc.url}"/>
  17. <property name="username" value="${jdbc.username}"/>
  18. <property name="password" value="${jdbc.password}"/>
  19. <!-- 配置初始化大小、最小、最大 -->
  20. <property name="initialSize" value="5" />
  21. <property name="minIdle" value="2" />
  22. <property name="maxActive" value="20" />
  23. <!-- 配置获取连接等待超时的时间 -->
  24. <property name="maxWait" value="60000" />
  25. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  26. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  27. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  28. <property name="minEvictableIdleTimeMillis" value="300000" />
  29. <property name="validationQuery" value="SELECT 'x'" />
  30. <property name="testWhileIdle" value="true" />
  31. <property name="testOnBorrow" value="false" />
  32. <property name="testOnReturn" value="false" />
  33. <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
  34. <property name="poolPreparedStatements" value="true" />
  35. <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
  36. <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
  37. <property name="filters" value="stat" />
  38. </bean>
  39. <!--创建sqlSessionFactory ==》 mybatis核心配置文件的读取-->
  40. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  41. <!--注入连接池-->
  42. <property name="dataSource" ref="dataSource"/>
  43. <!--加载mybatis全局配置文件-->
  44. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  45. <!--mapper.xml所在位置-->
  46. <property name="mapperLocations" value="classpath:com/shuai/mapper/xml/*.xml"/>
  47. <!--指定需要使用别名的实体类-->
  48. <property name="typeAliasesPackage" value="com.shuai.pojo"/>
  49. </bean>
  50. <!-- 扫描mybatis的mapper接口路径 -->
  51. <!-- 这个bean可以把我们的mapper接口直接扫描到. 直接把接口扫描完. 注册到spring的bean中 -->
  52. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  53. <!--给出需要扫描的Dao接口-->
  54. <property name="basePackage" value="com.shuai.mapper"/>
  55. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  56. </bean>
  57. <!--配置声明式事务-->
  58. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  59. <!--连接数据库连接池-->
  60. <property name="dataSource" ref="dataSource"/>
  61. </bean>
  62. </beans>

4、spring-mvc.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.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <!-- 开启注解扫描 -->
  13. <context:component-scan base-package="com.shuai"/>
  14. <!--开启SpringMvc注解-->
  15. <mvc:annotation-driven/>
  16. <!--3.配置jsp 显示ViewResolver视图解析器-->
  17. <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="prefix" value="/WEB-INF/pages/"/>
  19. <property name="suffix" value=".jsp"/>
  20. </bean>
  21. <!--过滤静态资源-->
  22. <mvc:default-servlet-handler/>
  23. </beans>

5、web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!--配置DispatcherServlet:这个是SpringMvc核心:请求分发器,前端控制器-->
  7. <servlet>
  8. <servlet-name>DispatcherServlet</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <!--声明配置文件位置-->
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:spring-mvc.xml</param-value>
  14. </init-param>
  15. <!--启动级别:1 意思就是项目启动时就加载这个servlet-->
  16. <load-on-startup>1</load-on-startup>
  17. </servlet>
  18. <!--
  19. 在SpringMvc中,/ /*
  20. / : 只匹配所有的请求,不会去匹配jsp页面
  21. /* : 匹配所有页面, 包括jsp页面
  22. -->
  23. <servlet-mapping>
  24. <servlet-name>DispatcherServlet</servlet-name>
  25. <url-pattern>/</url-pattern>
  26. </servlet-mapping>
  27. <!--防乱码-->
  28. <filter>
  29. <filter-name>encoding</filter-name>
  30. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  31. <init-param>
  32. <param-name>encoding</param-name>
  33. <param-value>utf-8</param-value>
  34. </init-param>
  35. </filter>
  36. <filter-mapping>
  37. <filter-name>encoding</filter-name>
  38. <url-pattern>/*</url-pattern>
  39. </filter-mapping>
  40. <!--配置监听器,在项目启动时,同时启动Spring工厂,使得Dao和Service以及Druid连接池启动-->
  41. <listener>
  42. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  43. </listener>
  44. <!--配置spring-mybatis文件路径-->
  45. <!--
  46. 如果这个地方不配置的话 <init-param>
  47. 这个里面要写总的spring配置的.xml文件
  48. 这是个大坑!!!
  49. -->
  50. <context-param>
  51. <param-name>contextConfigLocation</param-name>
  52. <param-value>classpath:spring-mybatis.xml</param-value>
  53. </context-param>
  54. </web-app>

总结:

现在才发现抄老师的代码是多么幸福的一件事!之前我都是跟着老师敲代码

现在,自己独立写代码,一步一个坑,一步一个坑!!

不过这些确实加深了我对SSM框架的理解!

加油吧少年!即使再小的帆也能远航!