导入pom依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>sys_service</artifactId><!--我的和你的不一样根据自己的地址写--> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>sys_service Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>8</maven.compiler.source><!--jdk改为8--> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- log4j 日志jar --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- 连接mysql5 的驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!-- 参考版本对应 http://www.mybatis.org/spring/ --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 数据源管理 使用了dbcp2数据 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- commons 文件上传jar --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 加入JSON转换工具 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies> <build> <finalName>springMVC01</finalName> </build> </project>
配置mybatis
<?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> <setting name="logImpl" value="LOG4J"/> </settings> </configuration>
mybatis中不再配置jdbc都交给spring处理,此配置文件中只放log4j日志
配置spring
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--加载属性文件--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--创建dbcp2数据源,此数据源可以替换为阿里的 德鲁伊--> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${pwd}"></property> </bean> <!--整合了slqSessionFactory包含了数据源(dataSource),配置文件(config)和映射文件(mapper)--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis.xml"></property> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> </bean> <!--扫描Mapper接口--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.book.mapper"></property> </bean> <!--扫描业务逻辑层注解--> <context:component-scan base-package="com.book"></context:component-scan> <!--引入事务管理器,管理指定的数据源--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" autowire="byName"> </bean> <!--把事务管理,变成增强(通知),同时指定了方法的事务传播机制--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update**" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="servicePointcut" expression="execution(* com.book.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"></aop:advisor> </aop:config> <!--引用springmvc配置文件--> <import resource="springmvc.xml"></import> </beans>
一定要记得在配置spring配置文件要引用springmvc配置文件。
配置springmvc
<?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:mvc="http://www.springframework.org/schema/mvc" 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 "> <context:component-scan base-package="com.book"></context:component-scan> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <!-- Date的日期转换器 --> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!--转发时的前后坠--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property><!--前缀--> <property name="suffix" value=""></property><!--后缀--> </bean> <!--设置存放图片的静态文件夹--> <mvc:resources mapping="/static/**" location="/static/"></mvc:resources> <!-- 配置multipartResolver,用于上传文件,使用spring的CommonsMultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxInMemorySize" value="5000000"></property> <property name="defaultEncoding" value="UTF-8"></property> </bean> </beans>
配置web
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <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:spring.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> <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> </web-app>
测试增删改查流程
1.编写实体类
2.编写mapper层
3.编写service层
4.编写controller层
5.网页测试
编写实体类
package com.book.bean; import com.alibaba.fastjson.annotation.JSONField; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; public class Bookmanage { private Integer id; private String name; private String author; @DateTimeFormat(pattern = "yyyy-MM-dd")//事件类型转换注解 @JSONField(format = "yyyy-MM-dd") private Date time; private Integer type; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } }
编写Mapper接口
package com.book.mapper; import com.book.bean.Bookmanage; import java.util.List; public interface BookmanageMapper { List<Bookmanage> getBookmanageList(Bookmanage bookmanage); int addBookmanage(Bookmanage bookmanage); Bookmanage getBookmanagebyid(int id); int updateBookmanage(Bookmanage bookmanage); int deleteBookmanagebyid(int id); }
mybatis实现
<?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.book.mapper.BookmanageMapper"> <select id="getBookmanageList" resultType="com.book.bean.Bookmanage" parameterType="com.book.bean.Bookmanage"> SELECT id,name,author,time,type FROM `bookmanage` <where> <if test="id!=null"> and id=#{id} </if> <if test="name!=null"> and name=#{name} </if> <if test="author!=null"> and author=#{author} </if> <if test="time!=null"> and time=#{time} </if> <if test="type!=null"> and type=#{type} </if> </where> </select> <insert id="addBookmanage" parameterType="com.book.bean.Bookmanage"> insert into bookmanage(name,author,time,type)value(#{name},#{author},#{time},#{type}) </insert> <select id="getBookmanagebyid" resultType="com.book.bean.Bookmanage" parameterType="int"> SELECT id,name,author,time,type FROM `bookmanage` where id=#{id} </select> <update id="updateBookmanage" parameterType="com.book.bean.Bookmanage"> update bookmanage <set> <if test="id!=null"> id=#{id}, </if> <if test="name!=null"> name=#{name}, </if> <if test="author!=null"> author=#{author}, </if> <if test="time!=null"> time=#{time}, </if> <if test="type!=null"> type=#{type}, </if> </set> where id=#{id} </update> <delete id="deleteBookmanagebyid" parameterType="int"> delete from bookmanage where id=#{id} </delete> </mapper>