一,导入相关依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>5.0.8.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <mysql.version>5.1.6</mysql.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <pagehelper.version>4.0.0</pagehelper.version> <mybatis.version>3.4.6</mybatis.version> <jackjson.version>2.9.7</jackjson.version> <druid.version>1.0.9</druid.version> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</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-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</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> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackjson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackjson.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId> org.aspectj</groupId > <artifactId> aspectjweaver</artifactId > <version> 1.7.4</version > </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> </dependencies>
二.可以先搭建spring框架
2.1创建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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理--> <context:component-scan base-package="com.liu.itcast" > <!--配置哪些注解不扫描--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!--Spring整合MyBatis框架--> <!-- 数据库连接池 --> <!-- 加载配置文件 --> <context:property-placeholder location="classpath*:*.properties" /> <!-- 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!--配置SqlSessionFactory工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <!--配置AccountDao接口所在包--> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.liu.itcast.web.demo.mapper"/> </bean> <!--配置Spring框架声明式事务管理--> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--配置事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" isolation="DEFAULT"/> </tx:attributes> </tx:advice> <!--配置AOP增强--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.liu.itcast.web.demo.service.impl.*ServiceImpl.*(..))"/> </aop:config> </beans>
三.springmvc的构建,因为springmvc 也是属于spring 的 ,其实就是在web下能读取spring的配置容器
3.1配置springmvc 的核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启注解扫描,只扫描Controller注解--> <context:component-scan base-package="com.liu.itcast"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!--配置的视图解析器对象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!-- <!–过滤静态资源–> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" />--> <!--开启SpringMVC注解的支持--> <mvc:annotation-driven/> </beans>
3.2配置web的wb.xml文件
<!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> <display-name>Archetype Created Web Application</display-name> <!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--设置配置文件的路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springmvc.xml配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <!--启动服务器,创建该servlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--解决中文乱码的过滤器--> <filter> <filter-name>characterEncodingFilter</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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
现在spring(包括springmvc)的基本完成了,可以写个小demo测试
测试代码
业务层:
import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService{ @Override public void test() { System.out.println("服务层代码调用"); } }
控制层:
package com.liu.itcast.web; import com.liu.itcast.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/demo") public class UserController { @Autowired private UserService userService; @RequestMapping("/test") public String test(){ userService.test(); return "ceshi"; } }
后台输出结果:服务层代码调用
页面显示结果:ceshi
四.接下来就是完成mybatis的整合
4.1数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao
jdbc.username=root
jdbc.password=root
其实在上面spring的配置文件已经将mybatis整合起来了,我们可以写个小的测试demo
mapper.xml
<?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.liu.itcast.mapper.TbContentMapper" > <resultMap id="BaseResultMap" type="com.liu.itcast.domain.TbContent" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="category_id" property="categoryId" jdbcType="BIGINT" /> <result column="title" property="title" jdbcType="VARCHAR" /> <result column="sub_title" property="subTitle" jdbcType="VARCHAR" /> <result column="title_desc" property="titleDesc" jdbcType="VARCHAR" /> <result column="url" property="url" jdbcType="VARCHAR" /> <result column="pic" property="pic" jdbcType="VARCHAR" /> <result column="pic2" property="pic2" jdbcType="VARCHAR" /> <result column="created" property="created" jdbcType="TIMESTAMP" /> <result column="updated" property="updated" jdbcType="TIMESTAMP" /> </resultMap> <resultMap id="ResultMapWithBLOBs" type="com.liu.itcast.domain.TbContent" extends="BaseResultMap" > <result column="content" property="content" jdbcType="LONGVARCHAR" /> </resultMap> <sql id="Example_Where_Clause" > <where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause" > <where > <foreach collection="example.oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List" > id, category_id, title, sub_title, title_desc, url, pic, pic2, created, updated </sql> <sql id="Blob_Column_List" > content </sql> <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs" parameterType="com.liu.itcast.domain.TbContentExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> , <include refid="Blob_Column_List" /> from tb_content <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.liu.itcast.domain.TbContentExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from tb_content <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Long" > select <include refid="Base_Column_List" /> , <include refid="Blob_Column_List" /> from tb_content where id = #{id,jdbcType=BIGINT} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" > delete from tb_content where id = #{id,jdbcType=BIGINT} </delete> <delete id="deleteByExample" parameterType="com.liu.itcast.domain.TbContentExample" > delete from tb_content <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="com.liu.itcast.domain.TbContent" > insert into tb_content (id, category_id, title, sub_title, title_desc, url, pic, pic2, created, updated, content) values (#{id,jdbcType=BIGINT}, #{categoryId,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, #{subTitle,jdbcType=VARCHAR}, #{titleDesc,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, #{pic,jdbcType=VARCHAR}, #{pic2,jdbcType=VARCHAR}, #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP}, #{content,jdbcType=LONGVARCHAR}) </insert> <insert id="insertSelective" parameterType="com.liu.itcast.domain.TbContent" > insert into tb_content <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="categoryId != null" > category_id, </if> <if test="title != null" > title, </if> <if test="subTitle != null" > sub_title, </if> <if test="titleDesc != null" > title_desc, </if> <if test="url != null" > url, </if> <if test="pic != null" > pic, </if> <if test="pic2 != null" > pic2, </if> <if test="created != null" > created, </if> <if test="updated != null" > updated, </if> <if test="content != null" > content, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=BIGINT}, </if> <if test="categoryId != null" > #{categoryId,jdbcType=BIGINT}, </if> <if test="title != null" > #{title,jdbcType=VARCHAR}, </if> <if test="subTitle != null" > #{subTitle,jdbcType=VARCHAR}, </if> <if test="titleDesc != null" > #{titleDesc,jdbcType=VARCHAR}, </if> <if test="url != null" > #{url,jdbcType=VARCHAR}, </if> <if test="pic != null" > #{pic,jdbcType=VARCHAR}, </if> <if test="pic2 != null" > #{pic2,jdbcType=VARCHAR}, </if> <if test="created != null" > #{created,jdbcType=TIMESTAMP}, </if> <if test="updated != null" > #{updated,jdbcType=TIMESTAMP}, </if> <if test="content != null" > #{content,jdbcType=LONGVARCHAR}, </if> </trim> </insert> <select id="countByExample" parameterType="com.liu.itcast.domain.TbContentExample" resultType="java.lang.Integer" > select count(*) from tb_content <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map" > update tb_content <set > <if test="record.id != null" > id = #{record.id,jdbcType=BIGINT}, </if> <if test="record.categoryId != null" > category_id = #{record.categoryId,jdbcType=BIGINT}, </if> <if test="record.title != null" > title = #{record.title,jdbcType=VARCHAR}, </if> <if test="record.subTitle != null" > sub_title = #{record.subTitle,jdbcType=VARCHAR}, </if> <if test="record.titleDesc != null" > title_desc = #{record.titleDesc,jdbcType=VARCHAR}, </if> <if test="record.url != null" > url = #{record.url,jdbcType=VARCHAR}, </if> <if test="record.pic != null" > pic = #{record.pic,jdbcType=VARCHAR}, </if> <if test="record.pic2 != null" > pic2 = #{record.pic2,jdbcType=VARCHAR}, </if> <if test="record.created != null" > created = #{record.created,jdbcType=TIMESTAMP}, </if> <if test="record.updated != null" > updated = #{record.updated,jdbcType=TIMESTAMP}, </if> <if test="record.content != null" > content = #{record.content,jdbcType=LONGVARCHAR}, </if> </set> <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExampleWithBLOBs" parameterType="map" > update tb_content set id = #{record.id,jdbcType=BIGINT}, category_id = #{record.categoryId,jdbcType=BIGINT}, title = #{record.title,jdbcType=VARCHAR}, sub_title = #{record.subTitle,jdbcType=VARCHAR}, title_desc = #{record.titleDesc,jdbcType=VARCHAR}, url = #{record.url,jdbcType=VARCHAR}, pic = #{record.pic,jdbcType=VARCHAR}, pic2 = #{record.pic2,jdbcType=VARCHAR}, created = #{record.created,jdbcType=TIMESTAMP}, updated = #{record.updated,jdbcType=TIMESTAMP}, content = #{record.content,jdbcType=LONGVARCHAR} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map" > update tb_content set id = #{record.id,jdbcType=BIGINT}, category_id = #{record.categoryId,jdbcType=BIGINT}, title = #{record.title,jdbcType=VARCHAR}, sub_title = #{record.subTitle,jdbcType=VARCHAR}, title_desc = #{record.titleDesc,jdbcType=VARCHAR}, url = #{record.url,jdbcType=VARCHAR}, pic = #{record.pic,jdbcType=VARCHAR}, pic2 = #{record.pic2,jdbcType=VARCHAR}, created = #{record.created,jdbcType=TIMESTAMP}, updated = #{record.updated,jdbcType=TIMESTAMP} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="com.liu.itcast.domain.TbContent" > update tb_content <set > <if test="categoryId != null" > category_id = #{categoryId,jdbcType=BIGINT}, </if> <if test="title != null" > title = #{title,jdbcType=VARCHAR}, </if> <if test="subTitle != null" > sub_title = #{subTitle,jdbcType=VARCHAR}, </if> <if test="titleDesc != null" > title_desc = #{titleDesc,jdbcType=VARCHAR}, </if> <if test="url != null" > url = #{url,jdbcType=VARCHAR}, </if> <if test="pic != null" > pic = #{pic,jdbcType=VARCHAR}, </if> <if test="pic2 != null" > pic2 = #{pic2,jdbcType=VARCHAR}, </if> <if test="created != null" > created = #{created,jdbcType=TIMESTAMP}, </if> <if test="updated != null" > updated = #{updated,jdbcType=TIMESTAMP}, </if> <if test="content != null" > content = #{content,jdbcType=LONGVARCHAR}, </if> </set> where id = #{id,jdbcType=BIGINT} </update> <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.liu.itcast.domain.TbContent" > update tb_content set category_id = #{categoryId,jdbcType=BIGINT}, title = #{title,jdbcType=VARCHAR}, sub_title = #{subTitle,jdbcType=VARCHAR}, title_desc = #{titleDesc,jdbcType=VARCHAR}, url = #{url,jdbcType=VARCHAR}, pic = #{pic,jdbcType=VARCHAR}, pic2 = #{pic2,jdbcType=VARCHAR}, created = #{created,jdbcType=TIMESTAMP}, updated = #{updated,jdbcType=TIMESTAMP}, content = #{content,jdbcType=LONGVARCHAR} where id = #{id,jdbcType=BIGINT} </update> <update id="updateByPrimaryKey" parameterType="com.liu.itcast.domain.TbContent" > update tb_content set category_id = #{categoryId,jdbcType=BIGINT}, title = #{title,jdbcType=VARCHAR}, sub_title = #{subTitle,jdbcType=VARCHAR}, title_desc = #{titleDesc,jdbcType=VARCHAR}, url = #{url,jdbcType=VARCHAR}, pic = #{pic,jdbcType=VARCHAR}, pic2 = #{pic2,jdbcType=VARCHAR}, created = #{created,jdbcType=TIMESTAMP}, updated = #{updated,jdbcType=TIMESTAMP} where id = #{id,jdbcType=BIGINT} </update> </mapper>
finish!