Spring Mybatis Mybatis 整合Spring SpringMvc
<!-- junit 测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> ? <!-- spring 核心jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> ? <!-- spring 测试jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.2.RELEASE</version> </dependency> ? <!-- spring jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.2.RELEASE</version> </dependency> ? <!-- spring事物 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.2.RELEASE</version> </dependency> ? <!-- aspectj切面编程的jar --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> ? ? <!-- c3p0 连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> ? <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> ? <!-- 添加mybatis与Spring整合的核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!-- mysql 驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> ? <!-- 日志打印相关的jar --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.2</version> </dependency> ? <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> ? <!-- spring web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.2.RELEASE</version> </dependency> ? <!-- spring mvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version> </dependency> ? <!-- web servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> </dependency> ? <!-- 添加json 依赖jar包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.0</version> </dependency> ? ? <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency>
<build> <finalName>ssm</finalName> ? <!-- Maven 项目 如果源代码(src/main/java)存在xml properties tld 等文件 maven 默认不会自动编译该文件到输出目录 如果要编译源代码中xml properties tld 等文件 需要显式配置resources 标签 --> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <include>**/*.tld</include> </includes> <filtering>false</filtering> </resource> </resources> ? <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> ? <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> ? <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.25</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <contextPath>/ssm</contextPath> </configuration> </plugin> </plugins> ? </build>
Spring.xml 监听器 编码过滤器 请求转发DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> ? <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ? <!-- 编码过滤 utf-8 --> <filter> <description>char encoding filter</description> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ? <!-- servlet请求分发器 --> <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:servlet-context.xml</param-value> </init-param> <!-- 表示启动容器时初始化该Servlet --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <!-- 这是拦截请求, /代表拦截所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> ? </web-app>
5.添加servlet-context.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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <!-- 扫描com.test.controller 下包 --> <context:component-scan base-package="com.test.controller" /> <!-- mvc 请求映射 处理器与适配器配置--> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> ? ? <!--配置视图解析器 默认的视图解析器- --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="contentType" value="text/html" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> ? <!-- 默认对静态资源放行 不做任何处理 框架提供的default-servlet-handler 默认对静态资源处理 响应给浏览器 --> <mvc:default-servlet-handler/> ? ? <!-- 文件上传解析器配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>4096</value> </property> </bean> ? </beans>
spring.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: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"> ? <!-- 配置扫描器 --> <context:component-scan base-package="com.test"> <!-- 排除@Controller 注解标注的Java 类 --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> ? ? <!-- 配置 加载jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> ? <!-- 数据源c3p0 配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> ? ? <!-- 事物控制 --> <aop:aspectj-autoproxy/> ? <!-- 事物管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> ? <!-- 声明事物通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="del*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> ? <aop:config> <aop:pointcut id="cut" expression="execution(* com.test.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="cut"></aop:advisor> </aop:config> ? <!-- mybatis 整合spring --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis.xml"></property> <property name="mapperLocations" value="classpath:com/shsxt/mappers/*.xml"></property> </bean> ? <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描com.test.dao这个包以及它的子包下的所有映射接口类 --> <property name="basePackage" value="com.test.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" /> </bean> </beans
mybatis.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> <typeAliases> <package name="com.test.vo"/> </typeAliases> ? ? <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> ? </configuration>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
log4j.properties
log4j.rootLogger=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
generatorConfig.xml 代码生成器配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--数据库驱动--> <classPathEntry location="C:/java/m2/repository/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/mybatis" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Model类存放位置--> <javaModelGenerator targetPackage="com.test.vo" targetProject="C:\java\idea_32\spring_mybatis\src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="com.test.mappers" targetProject="C:\java\idea_32\spring_mybatis\src\main\java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao类存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.test.dao" targetProject="C:\java\idea_32\spring_mybatis\src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> ? <table tableName="id_card" domainObjectName="IdCard" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="xx_product_category" domainObjectName="ProductCategory" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
添加Base(BaseMapper、BaseService、BaseQuery)
BaseMapper.java
package com.test.base; ? import org.springframework.dao.DataAccessException; ? import java.util.List; import java.util.Map; ? public interface BaseMapper<T,ID> { /** * 添加记录返回影响行数 */ public int save(T entity) throws DataAccessException; ? /** * 添加记录返回主键 */ public ID saveHasKey(T entity) throws DataAccessException; ? /** * 批量添加记录 */ public int saveBatch(List<T> entities) throws DataAccessException; ? ? /** * 详情查询 * @param id * @return */ public T queryById(ID id) throws DataAccessException; ? ? /** * 多条件列表查询 * @param baseQuery * @return */ public List<T> queryByParams(BaseQuery baseQuery) throws DataAccessException; ? /** * 更新单条记录 * @param entity * @return */ public int update(T entity) throws DataAccessException; ? ? /** * 批量更新 * @param map * @return */ public int updateBatch(Map<String, Object> map) throws DataAccessException; ? /** * 删除单条记录 * @param id * @return */ public int delete(ID id) throws DataAccessException; ? ? /** * 批量删除 * @param ids * @return */ public int deleteBatch(ID[] ids) throws DataAccessException; }
BaseService.java
package com.test.base; ? ? import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; ? import java.lang.reflect.Method; import java.util.List; import java.util.Map; ? public abstract class BaseService<T, ID> { ? @Autowired private BaseMapper<T, ID> baseMapper; ? public int save(T entity){ return baseMapper.save(entity); } ? public ID saveHasKey(T entity) { baseMapper.saveHasKey(entity); try { Method method= entity.getClass().getDeclaredMethod("getId",null); return (ID) method.invoke(entity,null); } catch (Exception e) { e.printStackTrace(); } return null; } ? public int saveBatch(List<T> entities){ return baseMapper.saveBatch(entities); } ? ? public T queryById(ID id){ return baseMapper.queryById(id); } ? public List<T> queryByParams(BaseQuery baseQuery){ return baseMapper.queryByParams(baseQuery); } ? public int update(T entity){ return baseMapper.update(entity); } ? public int updateBatch(Map<String,Object> map){ return baseMapper.updateBatch(map); } ? public int delete(ID id){ return baseMapper.delete(id); } ? public int deleteBatch(ID[] ids){ return baseMapper.deleteBatch(ids); } ? ? /** * 分页查询 * @param baseQuery * @return */ public PageInfo<T> queryForPage(BaseQuery baseQuery){ PageHelper.startPage(baseQuery.getPageNum(), baseQuery.getPageSize()); return new PageInfo<T>(queryByParams(baseQuery)); } ? }
BaseQuery.java
package com.test.base; ? public class BaseQuery { ? private Integer pageNum=1; private Integer pageSize=10; ? public Integer getPageNum() { return pageNum; } ? public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } ? public Integer getPageSize() { return pageSize; } ? public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } }
添加 UserDao、UserMapper.xml、UserService.java、UserController.java
UserDao.java
package com.test.dao; ? import com.test.vo.User; ? public interface UserDao { public User queryUserByUserId(Integer userId); }
UserMapper.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.test.dao.UserDao"> <select id="queryUserByUserId" parameterType="int" resultType="User"> select id, user_name as userName, user_pwd as userPwd, flag, create_time as createTime from user where id=#{userId} </select> </mapper>
UserService.java
package com.test.service; ? import com.test.dao.UserDao; import com.test.vo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; ? @Service public class UserService { ? @Autowired private UserDao userDao; ? public User queryUserByUserId(Integer userId){ return userDao.queryUserByUserId(userId); } }
UserController.java
package com.test.controller; ? import com.test.service.UserService; import com.test.vo.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; ? import javax.annotation.Resource; ? @Controller @RequestMapping("user") public class UserController { ? @Resource private UserService userService; ? @RequestMapping("queryUserByUserId") @ResponseBody public User queryUserByUserId(Integer userId){ return userService.queryUserByUserId(userId); } ? @RequestMapping("index") public String index(Integer userId, Model model){ User user = userService.queryUserByUserId(userId); model.addAttribute("user",user); return "user"; } }
7.配置启动命令
jetty:run -Djetty.port=8989
spring /mybatis/springMVC三大框架整合
原文:https://www.cnblogs.com/sunlili/p/11959163.html