整合mybatis-plus

master
星期八 5 years ago
parent 103de6f9f4
commit 02a15977ab
  1. 9
      pom.xml
  2. 4
      src/main/java/com/lq/code/dao/BaseDao.java
  3. 126
      src/main/java/com/lq/code/dao/impl/BaseDaoImpl.java
  4. 2
      src/main/java/com/lq/code/interceptor/PageInteceptor.java
  5. 13
      src/main/resources/spring-mybatis-config.xml

@ -19,7 +19,7 @@
<!-- spring版本号 --> <!-- spring版本号 -->
<spring.version>5.1.1.RELEASE</spring.version> <spring.version>5.1.1.RELEASE</spring.version>
<!-- mybatis版本号 --> <!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version> <mybatis.version>3.4.6</mybatis.version>
<!-- log4j日志文件管理包版本 --> <!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.12</slf4j.version> <slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version> <log4j.version>1.2.17</log4j.version>
@ -200,23 +200,24 @@
<!-- mybatis核心包 --> <!-- mybatis核心包 -->
<dependency> <!-- <dependency>
<groupId>org.mybatis</groupId> <groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId> <artifactId>mybatis</artifactId>
<version>${mybatis.version}</version> <version>${mybatis.version}</version>
</dependency> </dependency>
-->
<!-- mybatis/spring包 --> <!-- mybatis/spring包 -->
<dependency> <dependency>
<groupId>org.mybatis</groupId> <groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId> <artifactId>mybatis-spring</artifactId>
<version>1.2.2</version> <version>2.0.1</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus --> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
<!-- mybatis 增加框架(国产)--> <!-- mybatis 增加框架(国产)-->
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId> <artifactId>mybatis-plus</artifactId>
<version>3.1.1</version> <version>3.1.2</version>
</dependency> </dependency>
<!-- 导入java ee jar 包 --> <!-- 导入java ee jar 包 -->

@ -1,13 +1,15 @@
package com.lq.code.dao; package com.lq.code.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List; import java.util.List;
/** /**
* @author qi
* Created by qi on 2017/7/16. * Created by qi on 2017/7/16.
*/ */
public interface BaseDao<T> { public interface BaseDao<T> extends BaseMapper<T> {
/** /**
* 保存对象返回id * 保存对象返回id

@ -1,126 +0,0 @@
package com.lq.code.dao.impl;
import com.lq.code.dao.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
/**
* Created by qi on 2017/11/22.
*/
public class BaseDaoImpl<T> implements BaseDao<T> {
@Autowired(required = false)
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private Class<T> entityClass;
public BaseDaoImpl() {
Type genType = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
entityClass = (Class) params[0];
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
public void setNamedParameterJdbcTemplate(){
namedParameterJdbcTemplate=new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());
}
@Override
public Long save(T entity) {
if (namedParameterJdbcTemplate==null){
setNamedParameterJdbcTemplate();
}
SqlParameterSource parameterSource=new BeanPropertySqlParameterSource(entity);
return null;
}
@Override
public void update(T t) {
StringBuffer sql=new StringBuffer();
Field[] fields1= entityClass.getFields();
for (Field field:fields1){
field.setAccessible(true);
System.out.println(field);
}
Field[] fields=entityClass.getDeclaredFields();
for (Field field:fields){
System.out.println(field);
}
}
@Override
public T findOne(Long id) {
return null;
}
@Override
public List<T> findAll() {
return null;
}
@Override
public void delete(Long id) {
}
@Override
public void batchSave(List<T> entitylist) {
}
@Override
public void batchUpdate(List<T> entitylist) {
}
@Override
public void batchDelete(List<T> entitylist) {
}
private String makeSql(String sqlType){
StringBuffer sql=new StringBuffer();
Field[] fields=entityClass.getFields();
for (Field field:fields){
System.out.println(field);
}
return sql.toString();
}
//将类名转换为表面,开启驼峰命名
public String openHump(String entityName){
StringBuffer strBuffer=new StringBuffer();
char[] charArray=entityName.toCharArray();
for (int i=0;i<charArray.length;i++){
if (i==0){
strBuffer.append(charArray[i]);
continue;
}
if (Character.isUpperCase(charArray[i])){
strBuffer.append("_"+charArray[i]);
}else {
strBuffer.append(charArray[i]);
}
}
return strBuffer.toString().toUpperCase();
}
}

@ -26,7 +26,7 @@ import java.util.Properties;
//StatementHandler:数据库会话器,专门用于处理数据库会话,statement的执行操作,是一个接口; //StatementHandler:数据库会话器,专门用于处理数据库会话,statement的执行操作,是一个接口;
//metaObject:mybatis工具类,可以有效的读取或修改一些重要对象的属性 //metaObject:mybatis工具类,可以有效的读取或修改一些重要对象的属性
@Intercepts({@Signature(type = Executor.class,method = "query",args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class}), @Intercepts({@Signature(type = Executor.class,method = "query",args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class}) @Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class,Integer.class})
}) })
public class PageInteceptor implements Interceptor { public class PageInteceptor implements Interceptor {

@ -61,14 +61,19 @@
<!-- <property name="connectionInitSqls" value=""/>--> <!-- <property name="connectionInitSqls" value=""/>-->
</bean> </bean>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mybatis/mapping/*.xml"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" /> <property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 自动扫描mapping.xml文件 --> <!- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mapping/*.xml"></property> <!-- <property name="mapperLocations" value="classpath:mybatis/mapping/*.xml"></property>
</bean> </bean>-->
<!-- DAO接口所在包名,Spring会自动查找其下的类 --> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

Loading…
Cancel
Save