一、新建项目

image.png

image.png

二、项目目录

image.png
新增的项目没有 Java和resources目录,需要手动新建
image.png

三、整合mybatis和spring

1、导入依赖包

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-aspects</artifactId>
  5. <version>5.1.6.RELEASE</version>
  6. </dependency>
  7. <!-- Spring常用依赖 -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>5.1.6.RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. <version>5.1.47</version>
  17. </dependency>
  18. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  19. <dependency>
  20. <groupId>com.alibaba</groupId>
  21. <artifactId>druid</artifactId>
  22. <version>1.1.16</version>
  23. </dependency>
  24. <!-- spring-jdbc -->
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-jdbc</artifactId>
  28. <version>5.1.6.RELEASE</version>
  29. </dependency>
  30. <!-- spring+mybatis集成依赖 -->
  31. <dependency>
  32. <groupId>org.mybatis</groupId>
  33. <artifactId>mybatis-spring</artifactId>
  34. <version>1.3.1</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.mybatis</groupId>
  38. <artifactId>mybatis</artifactId>
  39. <version>3.4.6</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>junit</groupId>
  43. <artifactId>junit</artifactId>
  44. <version>4.13</version>
  45. </dependency>

2、mysql 链接

jdbc.properties

  1. #jdbc.properties
  2. jdbc.driver=com.mysql.jdbc.Driver
  3. jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
  4. jdbc.username=root
  5. jdbc.password=root
  6. jdbc.init=5
  7. jdbc.minIdle=10
  8. jdbc.maxActive=100

3、配置log4j

  1. # Global logging configuration
  2. log4j.rootLogger=DEBUG, stdout
  3. # MyBatis logging configuration...
  4. log4j.logger.org.mybatis.example.BlogMapper=TRACE
  5. # Console output...
  6. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  7. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

4、配置spring xml文件

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop.xsd
  16. ">
  17. </beans>

引入jdbc 外部配置文件

  1. <context:property-placeholder location="classpath:jdbc.properties" />

配置数据源

  1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  2. <!--基本配置-->
  3. <property name="driverClassName" value="${jdbc.driver}"/>
  4. <property name="url" value="${jdbc.url}"/>
  5. <property name="username" value="${jdbc.username}"/>
  6. <property name="password" value="${jdbc.password}"/>
  7. <!-- 配置初始化大小、最小、最大 -->
  8. <property name="initialSize" value="${jdbc.init}"/>
  9. <property name="minIdle" value="${jdbc.minIdle}"/>
  10. <property name="maxActive" value="${jdbc.maxActive}"/>
  11. <!-- 配置获取连接等待超时的时间 -->
  12. <property name="maxWait" value="60000"/>
  13. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  14. <property name="timeBetweenEvictionRunsMillis" value="60000"/>
  15. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  16. <property name="minEvictableIdleTimeMillis" value="300000"/>
  17. </bean>

注入sqlsessionfactory 工场

  1. <!-- 工厂bean:生成SqlSessionFactory -->
  2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  3. <!-- 注入连接池 -->
  4. <property name="dataSource" ref="dataSource"></property>
  5. <!-- 注入dao-mapper文件信息 ,如果映射文件和dao接口 同包且同名,则此配置可省略-->
  6. <property name="mapperLocations">
  7. <list>
  8. <value>classpath:mapper/*.xml</value>
  9. </list>
  10. </property>
  11. <!-- 为 dao-mapper文件中的实体 定义缺省包路径
  12. 如:<select id="queryAll" resultType="User"> 中 User类可以不定义包
  13. -->
  14. <property name="typeAliasesPackage" value="com.qf.entity"></property>
  15. </bean>

描述 mapper或者dao 接口路径

  1. <!-- mapperScannerConfigurer -->
  2. <bean id="mapperScannerConfigurer9" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  3. <!-- dao接口所在的包 如果有多个包,可以用逗号或分号分隔
  4. <property name="basePackage" value="com.a.dao,com.b.dao"></property>
  5. -->
  6. <property name="basePackage" value="com.qf.dao"></property>
  7. <!-- 如果工厂中只有一个SqlSessionFactory的bean,此配置可省略 -->
  8. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  9. </bean>

事务配置:

  1. <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  2. <property name="dataSource" ref="dataSource"></property>
  3. </bean>

定义事务通知:

  1. <tx:advice id="txManager" transaction-manager="tx">
  2. <tx:attributes>
  3. <!--<tx:method name="insertUser" rollback-for="Exception" isolation="DEFAULT"
  4. propagation="REQUIRED" read-only="false"/>-->
  5. <!-- 以User结尾的方法,切入此方法时,采用对应事务实行 连接点-->
  6. <tx:method name="*" rollback-for="Exception"/>
  7. </tx:attributes>
  8. </tx:advice>

插入切点:

  1. 切面
  2. <aop:config>
  3. <aop:pointcut expression="execution(* com.qf.service.*.*(..))" id="pc"/>
  4. <!-- 组织切面 -->
  5. <aop:advisor advice-ref="txManager" pointcut-ref="pc"/>
  6. </aop:config>

5、编写dao

  1. public interface UserDao {
  2. List<User> findAll();
  3. Integer insert(User user);
  4. }

编写bean

  1. public class User implements Serializable {
  2. private Integer id;
  3. private String name;
  4. private String password;
  5. private String sex;
  6. private Date birthday;
  7. private Date registTime;

编写mapper

  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.qf.dao.UserDao">
  5. <select id="findAll" resultType="com.qf.entity.User">
  6. select * from t_users
  7. </select>
  8. <insert id="insert" parameterType="com.qf.entity.User">
  9. INSERT INTO t_users VALUES(#{id},#{name},#{password},#{sex},#{birthday},#{registTime})
  10. </insert>
  11. </mapper>

6、编写service

  1. public interface IUserService {
  2. List<User> findAll();
  3. Integer insert();
  4. }
  1. public class UserServiceImpl implements IUserService {
  2. private UserDao userDao;
  3. @Override
  4. public List<User> findAll() {
  5. return userDao.findAll();
  6. }
  7. @Override
  8. public Integer insert() {
  9. User user = new User();
  10. user.setName("疯子");
  11. user.setPassword("1111");
  12. user.setSex("nv");
  13. user.setBirthday(new Date());
  14. user.setRegistTime(new Date());
  15. Integer insert = userDao.insert(user);
  16. int i= 1/0;
  17. return insert;
  18. }
  19. public void setUserDao(UserDao userDao) {
  20. this.userDao = userDao;
  21. }
  22. }

四、整合MVC

1、导入依赖

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-webmvc</artifactId>
  4. <version>5.1.6.RELEASE</version>
  5. </dependency>
  6. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  7. <dependency>
  8. <groupId>javax.servlet</groupId>
  9. <artifactId>javax.servlet-api</artifactId>
  10. <version>3.1.0</version>
  11. <scope>provided</scope>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
  14. <dependency>
  15. <groupId>javax.servlet</groupId>
  16. <artifactId>jstl</artifactId>
  17. <version>1.2</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>javax.servlet</groupId>
  21. <artifactId>jsp-api</artifactId>
  22. <version>2.0</version>
  23. <scope>provided</scope>
  24. </dependency>
  25. <dependency>
  26. <groupId>com.alibaba</groupId>
  27. <artifactId>fastjson</artifactId>
  28. <version>1.2.54</version>
  29. </dependency>

2、配置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. </web-app>

配置默认的servlet控制器

  1. <servlet>
  2. <servlet-name>mvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <!-- 局部参数:声明配置文件位置 -->
  5. <init-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath:mvc.xml</param-value>
  8. </init-param>
  9. <!-- Servlet启动时刻:可选 1 代表饿汉式 默认 就是懒汉式 -->
  10. <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>mvc</servlet-name>
  14. <url-pattern>/</url-pattern>
  15. </servlet-mapping>

配置字符编码集

  1. <filter>
  2. <filter-name>encoding</filter-name>
  3. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>utf-8</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>encoding</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>

配置spring beans

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  11. </beans>

启动注解和注解扫描包路径

  1. <!-- 告知springmvc 哪些包中 存在 被注解的类 -->
  2. <context:component-scan base-package="com.qf.controller"></context:component-scan>
  3. <!-- 注册注解开发驱动 启动注解 -->
  4. <mvc:annotation-driven></mvc:annotation-driven>

配置文件后缀

  1. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  2. <!-- 前缀 -->
  3. <property name="prefix" value="/"></property>
  4. <!-- 后缀 -->
  5. <property name="suffix" value=".jsp"></property>
  6. </bean>

配置静态访问路径

  1. <!-- <mvc:default-servlet-handler/>-->
  2. <mvc:resources mapping="/cls/**" location="/html/"/>
  3. <mvc:annotation-driven>
  4. <!--解决响应中文乱码-->
  5. <mvc:message-converters>
  6. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  7. <constructor-arg value="UTF-8" />
  8. </bean>
  9. </mvc:message-converters>
  10. </mvc:annotation-driven>

启动 application.xml

  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:applicationContext.xml</param-value>
  7. </context-param>

五 整合分页插件

1、导入依赖包

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper</artifactId>
  4. <version>5.1.10</version>
  5. </dependency>

2、添加配置文件到sqlsessionfactory

  1. <property name="plugins">
  2. <array>
  3. <bean class="com.github.pagehelper.PageInterceptor">
  4. <property name="properties">
  5. <value>
  6. helperDialect=mysql
  7. reasonable=true
  8. supportMethodsArguments=true
  9. params=count=countSql
  10. autoRuntimeDialect=true
  11. </value>
  12. </property>
  13. </bean>
  14. </array>
  15. </property>

六、整合文件上传

1、导入依赖包

  1. <dependency>
  2. <groupId>commons-io</groupId>
  3. <artifactId>commons-io</artifactId>
  4. <version>2.4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>commons-fileupload</groupId>
  8. <artifactId>commons-fileupload</artifactId>
  9. <version>1.3.3</version>
  10. <exclusions>
  11. <exclusion>
  12. <groupId>javax.servlet</groupId>
  13. <artifactId>servlet-api</artifactId>
  14. </exclusion>
  15. </exclusions>
  16. </dependency>

2、添加配置

  1. <bean id="multipartResolver"
  2. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  3. <!-- 最大可上传的文件大小 单位:byte 超出后会抛出MaxUploadSizeExceededException异常,可以异常解析器捕获 -->
  4. <property name="maxUploadSize" value="1048576"></property>
  5. </bean>

七 验证码

1 导入依赖

  1. <dependency>
  2. <groupId>com.github.penggle</groupId>
  3. <artifactId>kaptcha</artifactId>
  4. <version>2.3.2</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>javax.servlet</groupId>
  8. <artifactId>javax.servlet-api</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>

2 web.xml添加配置

  1. <servlet>
  2. <servlet-name>cap</servlet-name>
  3. <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
  4. <init-param>
  5. <param-name>kaptcha.border</param-name>
  6. <param-value>no</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>kaptcha.textproducer.char.length</param-name>
  10. <param-value>4</param-value>
  11. </init-param>
  12. <init-param>
  13. <param-name>kaptcha.textproducer.char.string</param-name>
  14. <param-value>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</param-value>
  15. </init-param>
  16. <init-param>
  17. <param-name>kaptcha.background.clear.to</param-name>
  18. <param-value>211,229,237</param-value>
  19. </init-param>
  20. <init-param>
  21. <!-- session.setAttribute("captcha","验证码") -->
  22. <param-name>kaptcha.session.key</param-name>
  23. <param-value>captcha</param-value>
  24. </init-param>
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>cap</servlet-name>
  28. <url-pattern>/captcha</url-pattern>
  29. </servlet-mapping>

八 整合log4j2

1、导入依赖

  1. <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
  2. <dependency>
  3. <groupId>org.apache.logging.log4j</groupId>
  4. <artifactId>log4j-slf4j-impl</artifactId>
  5. <version>2.11.2</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
  8. <dependency>
  9. <groupId>org.apache.logging.log4j</groupId>
  10. <artifactId>log4j-web</artifactId>
  11. <version>2.11.2</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-jcl -->
  14. <dependency>
  15. <groupId>org.apache.logging.log4j</groupId>
  16. <artifactId>log4j-jcl</artifactId>
  17. <version>2.11.2</version>
  18. </dependency>

2、导入配置

  1. <listener>
  2. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  3. </listener>
  4. <listener>
  5. <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
  6. </listener>
  7. <context-param>
  8. <param-name>log4jConfiguration</param-name>
  9. <!--默认是classpath下的log4j2.xml-->
  10. <param-value>classpath:log4j2.xml</param-value>
  11. </context-param>

3、日志配置信息

error—warn-info—dubeg

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration status="WARN" monitorInterval="30">
  3. <Properties>
  4. <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t-%L] %-5level
  5. %logger{36} - %msg%n</property>
  6. </Properties>
  7. <Appenders>
  8. <!--这个输出控制台的配置 -->
  9. <Console name="Console" target="SYSTEM_OUT">
  10. <!-- 控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
  11. <ThresholdFilter level="trace" onMatch="ACCEPT"
  12. onMismatch="DENY" />
  13. <PatternLayout pattern="${PATTERN}" />
  14. </Console>
  15. </Appenders>
  16. <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效 -->
  17. <Loggers>
  18. <!-- 配置日志的根节点 -->
  19. <root level="debug">
  20. <appender-ref ref="Console" />
  21. </root>
  22. </Loggers>
  23. </Configuration>