目录
1丶项目结构
?2丶所需依赖
3丶配置mybatis-config.xml文件
4丶配置springmvc-servlet.xml文件
5丶配置database.properties文件
6丶配置applicationContext-mybatis.xml文件
7丶配置web.xml文件
8丶运行效果
? ? ? ? 8.1丶首页界面
? ? ? ? 8.2丶?组合查询+分页
? ? ? ? 8.3丶添加
? ? ? ? 8.4丶修改?
? ? ? ? 8.5丶删除?
1丶项目结构
?2丶所需依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!-- 引入数据库连接池,c3p0,dbcp,druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- jedis 使用redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
3丶配置mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--严格区分大小写,空格-->
<!--STDOUT_LOGGING标准的日志工厂实现-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--autoMappingBehavior开启resultMap自动装配 普通列自动映射,对象列不自动映射-->
<setting name="autoMappingBehavior" value="FULL"/>
<!--开启二级缓存(Mapper)-->
<!-- <setting name="cacheEnabled" value="true"/>-->
</settings>
<typeAliases>
<!--扫描实体类中的别名,扫描后映射文件查询的返回值可以不写entity包-->
<package name="entity"/>
</typeAliases>
</configuration>
4丶配置springmvc-servlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--支持mvc注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--扫描Controller包-->
<context:component-scan base-package="controller"></context:component-scan>
<!--放行jsp所有静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--配置文件上传组装说明书-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置文件上传最大大小(字节)-->
<property name="maxUploadSize" value="500000"></property>
<!--设置文件上传格式-->
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>
5丶配置database.properties文件
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/t139test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
user=root
password=123123
minIdle=45
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
6丶配置applicationContext-mybatis.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描注解-->
<context:component-scan base-package="service,mapper"/>
<!--读取数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<!-- JNDI获取数据源(使用dbcp连接池) -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" scope="singleton">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxIdle" value="${maxIdle}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxWait" value="${maxWait}"/>
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/>
<property name="removeAbandoned" value="${removeAbandoned}"/>
<!-- sql 心跳 -->
<!--开启Evict的定时校验,循环校验 -->
<property name= "testWhileIdle" value="true"/>
<!-- 在进行borrowObject处理时,会对拿到的 连接进行校验-false-->
<property name= "testOnBorrow" value="false"/>
<!-- 在进行ruturnObject处理时,会对返回的连接进行校验-false -->
<property name= "testOnReturn" value="false"/>
<!-- 校验使用的sql语句,validatetionQuery,复杂的校验sql会影响性能 -->
<property name= "validationQuery" value="select 1"/>
<!-- 定义Evict的时间间隔,单位:毫秒 -->
<property name= "timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置每次校验连接的数量,一般等于maxActive -->
<property name= "numTestsPerEvictionRun" value="${maxActive}"/>
</bean>
<!--事务管理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置mybatis sqlsessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--接口扫描包-->
<!--优先扫描接口中的注解,没有的话会自动去找xml映射文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean>
</beans>
7丶配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0">
<!--关联spring核心配置文件,让其生效-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<!--过滤乱码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--整个项目的监听器,如果缺失那么整个项目的配置文件将会找不到-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
8丶运行效果
? ? ? ? 8.1丶首页界面
? ? ? ? 8.2丶?组合查询+分页
? ? ? ? 8.3丶添加
?
?
? ? ? ? 8.4丶修改?
?
?
? ? ? ? 8.5丶删除?
?
?
|