mybatis pageHelper + 通用mapper的使用

来源:互联网 发布:iphone6s数据漫游 编辑:程序博客网 时间:2024/05/20 20:56

为了加快开发速度选择使用以下插件

源码地址,包含了官方开发文档:http://git.oschina.net/free/Mapper

主要配置结构雏形,待优化

2017-09-05 20:44:30 已将项目中使用到mapper的部分精简打包,有空再更新

http://git.oschina.net/zx1323/ssm-collection

maven依赖 中添加

<!--分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.0.0</version></dependency><!-- 通用mapper --><dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>3.4.2</version></dependency>

spring-core 配置

<?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" xmlns:util="http://www.springframework.org/schema/util"default-autowire="byName"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><context:component-scan base-package="com.ssm.collection.core" /><tx:annotation-driven /><import resource="classpath:datasource-context.xml" /><bean id="mybatisSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation"><value>classpath:spring-mybaits.xml</value></property><!-- mapper扫描 --><property name="mapperLocations" value="classpath*:com/ssm/collection/core/**/*Mapper.xml" /></bean><bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 扫描不到实体 会报错 --><property name="basePackage" value="com.ssm.collection.core.*" /><property name="markerInterface" value="com.ssm.collection.core.common.mapper.MyBaseMapper" /><property name="sqlSessionFactoryBeanName" value="mybatisSqlSessionFactory"></property></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"><constructor-arg index="0" ref="mybatisSqlSessionFactory" /></bean><aop:aspectj-autoproxy /><aop:config><aop:pointcut id="appService" expression="execution(* com.ssm.collection.core.service..*Service*.*(..))" /><aop:advisor advice-ref="txAdvice" pointcut-ref="appService" /></aop:config><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes>  <tx:method name="insert*" propagation="REQUIRED" />             <tx:method name="update*" propagation="REQUIRED" />      <tx:method name="del*" propagation="REQUIRED" />  <tx:method name="select*" read-only="true"   propagation="SUPPORTS" /><tx:method name="find*" read-only="true" propagation="SUPPORTS"/><tx:method name="get*" read-only="true" propagation="SUPPORTS"/><tx:method name="count*" propagation="SUPPORTS" read-only="true" />  <tx:method name="*" read-only="true" propagation="SUPPORTS"/></tx:attributes></tx:advice><!-- Transactions management --><bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /><qualifier value="transactionManager" /></bean><tx:annotation-driven transaction-manager="transactionManager" /></beans>
datasource-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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"default-autowire="byName"><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="${JDBC.ssm.Driver}" /><property name="jdbcUrl" value="${JDBC.ssm.URL}"/><property name="user" value="${JDBC.ssm.User}" /><property name="password" value="${JDBC.ssm.Password}" /><property name="minPoolSize" value="${JDBC.ssm.MinPoolSize}" /><property name="maxPoolSize" value="${JDBC.ssm.MaxPoolSize}" /><property name="initialPoolSize" value="${JDBC.ssm.InitialPoolSize}" /><property name="maxIdleTime" value="${JDBC.ssm.MaxIdleTime}" /><property name="acquireIncrement" value="${JDBC.ssm.AcquireIncrement}" /><property name="acquireRetryAttempts" value="${JDBC.ssm.AcquireRetryAttempts}" /><property name="acquireRetryDelay" value="${JDBC.ssm.AcquireRetryDelay}" /><property name="idleConnectionTestPeriod" value="${JDBC.ssm.IdleConnectionTestPeriod}" /><property name="checkoutTimeout" value="${JDBC.ssm.CheckoutTimeout}" /></bean></beans>

system-config.properties

JDBC.Driver=com.mysql.jdbc.DriverJDBC.URL=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxxJDBC.User=JDBC.Password=JDBC.MinPoolSize=0JDBC.MaxPoolSize=5JDBC.InitialPoolSize=0JDBC.MaxIdleTime=25000JDBC.AcquireIncrement=1JDBC.AcquireRetryAttempts=30JDBC.AcquireRetryDelay=1000JDBC.TestConnectionOnCheckin=trueJDBC.AutomaticTestTable=c3p0TestTableJDBC.IdleConnectionTestPeriod=18000JDBC.CheckoutTimeout=0
spring-mybatisd.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><setting name="cacheEnabled" value="false" /><setting name="lazyLoadingEnabled" value="true" /><setting name="aggressiveLazyLoading" value="false" /></settings><typeAliases><package name="com.ssm.collection.core" /></typeAliases><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"><!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 --> <!-- 4.0.0以后版本可以不设置该参数 -->  <property name="helperDialect" value="mysql" /></plugin></plugins></configuration>


实体中  引用别的类当字段 需加上@Transient

===============================================

下载项目后,要修改数据库配置,导入sql 后,即可正常跑单元测试

由于项目中参杂着另外一种在mapper文件中手写sql的用法,好处是传参、返回结果时,参数都可以是map结构,不用new实体,所以做了保留

dao包下的类:是需要自己在   mapper.xml文件中写sql的实现方式(用户的批量查询,就是这种方式实现的)

service包下的类:是通用mapper的实现方式(根据user  id   查找用户,就是这种方式实现的)

entity包下就是对应数据库表的实体


BaseActionContorller:利用通用mapper插件,实现对但表的CRUD

UserController:继承BaseActionContorller后,单表CRUD基本上很容易了(实际使用中肯定会复杂很多,比如新增,更新时密码的md5加密)


平时开发基本上 只需要在   UserService      UserServiceImpl     两个类中编写代码

博文写的烂,轻喷


原创粉丝点击