标签:ret rri demo column tomcat zha iba protected 工具
一.Tkmybatis的好处
Tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效。这个插件里面封装好了我们需要用到的很多sql语句,不过这个插件是通过我们去调用它封装的各种方法来实现sql语句的效果。对于单表查询不需要写SQL语句,这样就不用像mybatis那样每次写一个接口就要写一条sql语句。这样大大减少了我们的工作量。
拓展:IDEA中使用mybatis-generator自动生成mapper和pojo文件
使用maven命令即可使用:mvn mybatis-generator:generate
二.搭建与使用
1、首先我们添加tk.mybatis的依赖包
-
-
-
<groupId>tk.mybatis</groupId>
-
<artifactId>mapper</artifactId>
-
-
2、然后去添加一个UserInfo实体类
-
-
-
-
-
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "SELECT LAST_INSERT_ID()")
-
-
-
-
-
-
-
拓展:SpringBoot的@GeneratedValue的参数设置
默认是可以不加参数的,但是如果数据库控制主键自增(auto_increment), 不加参数就会报错。
@GeneratedValue(strategy=GenerationType.IDENINY)
PS:@GeneratedValue注解的strategy属性提供四种值:
-AUTO主键由程序控制, 是默认选项 ,不设置就是这个
-IDENTITY 主键由数据库生成, 采用数据库自增长, Oracle不支持这种方式
-SEQUENCE 通过数据库的序列产生主键, MYSQL 不支持
3、有两种方式可以扫描文件
(1)新建配置扫描类文件
MyBatisConfig.class:
-
package com.lkt.Professional.mapper.mybatis;
-
import org.apache.ibatis.session.SqlSessionFactory;
-
import org.mybatis.spring.SqlSessionFactoryBean;
-
import org.springframework.context.annotation.Bean;
-
public class MyBatisConfig {
-
@Bean(name = "sqlSessionFactory")
-
public SqlSessionFactory sqlSessionFactoryBean(){
-
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
-
-
bean.setTypeAliasesPackage("com.lkt.Professional.entity");
-
-
-
-
-
-
-
-
throw new RuntimeException(e);
-
-
-
MyBatisMapperScannerConfig.class:
-
package com.lkt.Professional.mapper.mybatis;
-
import java.util.Properties;
-
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import com.lkt.Professional.MyMappers.MyMapper;
-
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
-
-
-
@AutoConfigureAfter(MyBatisConfig.class)
-
public class MyBatisMapperScannerConfig {
-
-
public MapperScannerConfigurer mapperScannerConfigurer(){
-
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
-
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
-
mapperScannerConfigurer.setBasePackage("com.lkt.Professional.mapper.mybatis");
-
Properties properties = new Properties();
-
properties.setProperty("mappers", MyMapper.class.getName());
-
properties.setProperty("notEmpty", "false");
-
properties.setProperty("IDENTITY", "MYSQL");
-
-
mapperScannerConfigurer.setProperties(properties);
-
return mapperScannerConfigurer;
-
-
(2)在启动类中设置扫描
-
-
-
import javax.sql.DataSource;
-
-
import org.apache.ibatis.session.SqlSessionFactory;
-
import org.mybatis.spring.SqlSessionFactoryBean;
-
import tk.mybatis.spring.annotation.MapperScan;
-
-
import org.slf4j.LoggerFactory;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.boot.builder.SpringApplicationBuilder;
-
import org.springframework.boot.context.properties.ConfigurationProperties;
-
import org.springframework.boot.web.servlet.ServletComponentScan;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.core.io.DefaultResourceLoader;
-
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
-
-
-
-
-
@MapperScan("com.java.aney.mapper")
-
public class Application {
-
private static Logger logger = LoggerFactory.getLogger(Application.class);
-
-
protected SpringApplicationBuilder configure(
-
SpringApplicationBuilder application) {
-
return application.sources(Application.class);
-
-
-
-
@ConfigurationProperties(prefix = "spring.datasource")
-
public DataSource dataSource() {
-
return new org.apache.tomcat.jdbc.pool.DataSource();
-
-
-
-
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
-
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
-
sqlSessionFactoryBean.setDataSource(dataSource());
-
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
-
sqlSessionFactoryBean.setMapperLocations(resolver
-
.getResources("classpath:/mybatis/*.xml"));
-
-
sqlSessionFactoryBean.setConfigLocation(
-
new DefaultResourceLoader().getResource("classpath:mybatis-config.xml"));
-
return sqlSessionFactoryBean.getObject();
-
-
-
public static void main(String[] args) {
-
SpringApplication.run(Application.class);
-
-
-
4、新建公共父类BaseService
BaseService:(把BaseService文件存放在mapper文件夹的同级目录或者上级目录,如果扫描到了BaseService会出现报错)
-
package com.java.aney.service;
-
-
-
-
import com.github.pagehelper.PageInfo;
-
import com.java.aney.model.QueryExample;
-
-
import tk.mybatis.mapper.entity.Example;
-
-
public interface BaseServices<T, ID> {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Integer batchAdd(List<T> list);
-
-
-
-
-
-
-
-
Integer deleteById(ID id);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Integer updateByPrimaryKey(T t);
-
-
-
-
-
-
-
-
Integer batchUpdateByPrimaryKey(List<T> list);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Integer updateByExampleSelective(QueryExample<T> queryExample);
-
-
-
-
-
-
-
-
-
-
PageInfo<T> findPage(T t, Integer pageNum, Integer pageSize);
-
-
List<T> findByExample(Example example);
-
-
-
-
-
-
-
-
-
Integer updateByExampleSelective(T record, Example query);
-
-
-
-
-
-
-
-
-
Integer updateByExampleSelective(T record, T query);
-
-
-
-
-
-
-
-
Integer findCount(T record);
-
-
-
-
-
-
-
-
Integer findCountByExample(Example query);
-
5、新建公共封装SQL语句条件类
-
package com.java.aney.model;
-
-
public class QueryExample<T> {
-
-
-
-
-
-
-
-
-
-
-
public void setRecord(T record) {
-
-
-
-
public Object getExample() {
-
-
-
-
public void setExample(Object example) {
-
-
-
6、新建BaseServicesImpl实现父类BaseService
-
?package com.java.aney.service;
-
-
public abstract class BaseServicesImpl<T, ID> implements BaseServices<T, ID> {
-
-
protected final Logger logger = LoggerFactory.getLogger(getClass());
-
-
public abstract Mapper<T> getMapper();
-
-
-
@Transactional(rollbackFor = Exception.class)
-
public Integer add(T t) {
-
return getMapper().insertSelective(t);
-
-
-
-
@Transactional(rollbackFor = Exception.class)
-
public Integer deleteById(ID id) {
-
return getMapper().deleteByPrimaryKey(id);
-
-
-
拓展:tk.mybatis单表条件拼装SQL
链接:mybatis Example条件查询
tk mybatis update 各种类型
demo见方法如下:
-
-
@RequestMapping(value="bootgridpage",produces="application/json;charset=UTF-8")
-
-
public BootgridPageInfoSet fenye(int current,int rowCount,String sort,String nane,String ph ){
-
PageHelper.startPage(current,rowCount);
-
Example example = new Example(CcompareccicModel.class);
-
String by=Jsonutil.getsortby(sort);
-
example.setOrderByClause(by);
-
Example.Criteria criteria = example.createCriteria();
-
if (StringUtil.isNotEmpty(nane)) {
-
criteria.andLike("xm", "%" + nane + "%");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
List<CcompareccicModel> list=service.selectByExample(example);
-
new BootgridPageInfoSet<CcompareccicModel>(list);
-
return new BootgridPageInfoSet<CcompareccicModel>(list);
-
7、新建子类UserService继承BaseServicesImpl<AccountUser, Integer>,并重写方法
-
-
public class UserService extends BaseServicesImpl<User, Integer> {
-
-
-
private UserMapper userMapper;
-
-
-
public Mapper<AccountUser> getMapper() {
-
-
-
-
public AccountUser queryUserName(String userName) {
-
AccountUser user = userMapper.selectUserName(userName);
-
-
-
-
-
8、新建mapper接口继承父接口Mapper<T>
-
-
public interface UserMapper extends Mapper<User> {
-
-
-
-
-
-
-
public User selectUserName(String userName);
-
拓展:Mapper接口的声明如下,可以看到Mapper接口实现了所有常用的方法:
-
public interface Mapper<T> extends
-
-
-
-
-
-
9、新建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.lkt.Professional.UserMapper">
-
<resultMap id="BaseResultMap" type="com.model.User">
-
<id column="id" jdbcType="VARCHAR" property="id"/>
-
<result column="code" jdbcType="VARCHAR" property="code"/>
-
<result column="user_name" jdbcType="INTEGER" property="UserName"/>
-
<result column="initime" jdbcType="TIMESTAMP" property="initime"/>
-
<association property="userIdMap" column="user_id" foreignColumn="id" notNullColumn="user_name" javaType="map">
-
<id column="id" property="id"/>
-
<result column="user_name" property="name"/>
-
-
-
-
<select id="getUserDetail" resultMap="BaseResultMap" parameterType="String">
-
-
-
注意:右击application跑起来,如果报出有关mysql或者sql语句的错误
(1)检查application.properties文件数据库配置是否正确;
(2)检查bean(实体类)的类名是否与数据库表名一致,不一致是否添加@Table(name = "表名")注解声明;
(3)检查bean的变量名是否与该表名的列名一致,不一致是否添加@Column(name = "列名")注解声明。
SpringBoot集成TkMybatis插件 (二)
标签:ret rri demo column tomcat zha iba protected 工具
原文地址:https://www.cnblogs.com/panchanggui/p/10754355.html