官方地址:https://mapperhelper.github.io/docs/
目录
3.1 @GeneratedValue(generator = "JDBC")
3.2 @GeneratedValue(strategy = GenerationType.IDENTITY)
1 maven依赖jar
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
通用mapper是需要依赖persistence/persistence-api jar包,但是我们可以不配置。
其他的依赖和Mybatis是一样的。也需要mybatis-spring,mybatis等等
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hww</groupId>
<artifactId>tkmapperdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!--注解的扫描等,还有基础的bean aop core等核心spring包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!--spring orm 包含连接数据库相关的包,spring-jdbc spring-tx spring-orm-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!--第三方数据库连接池的包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!--日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
2. 配置
与mybatis的区别就是 mapper接口扫描器,这里与mybatis的区别就是将 org.mybatis.spring.mapper.MapperScannerConfigurer修改为tk.mybatis.spring.mapper.MapperScannerConfigurer。就是将org变为tk
这里制定了一个mappers=tk.mybatis.mapper.common.Mapper。这个是用来指定使用的通用mapper接口。
eg. ClientDao就继承了tk.mybatis.mapper.common.Mapper接口中的所有方法。
指定需要用的mapper接口,默认就是tk.mybatis.mapper.common.Mapper。这个接口中包含了所有通用mapper的方法,所以如果用tk.mybatis.mapper.common.Mapper,可以不配置prperties属性
package com.hww.dao;
import com.hww.bean.Client;
import tk.mybatis.mapper.common.Mapper;
public interface ClientDao extends Mapper<Client> {
}
<!--通用mapper
这里与mybatis的区别就是将 org.mybatis.spring.mapper.MapperScannerConfigurer
修改为 tk.mybatis.spring.mapper.MapperScannerConfigurer
-->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hww.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSession"></property>
<!--1. 指定需要用的mapper接口,默认就是tk.mybatis.mapper.common.Mapper
这个接口中包含了所有通用mapper的方法,所以如果用tk.mybatis.mapper.common.Mapper,可以不配置prperties属性
2. 如果我们的mapper接口想继承该接口,我们可以指定我们需要继承的接口。
-->
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>
完整配置
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:config.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transationManager"></tx:annotation-driven>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--通用mapper
这里与mybatis的区别就是将 org.mybatis.spring.mapper.MapperScannerConfigurer
修改为 tk.mybatis.spring.mapper.MapperScannerConfigurer
-->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hww.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSession"></property>
<!--1. 指定需要用的mapper接口,默认就是tk.mybatis.mapper.common.Mapper
这个接口中包含了所有通用mapper的方法,所以如果用tk.mybatis.mapper.common.Mapper,可以不配置prperties属性
2. 如果我们的mapper接口想继承该接口,我们可以指定我们需要继承的接口。
-->
<property name="properties">
<value>
mappers=tk.mybatis.mapper.common.Mapper
</value>
</property>
</bean>
</beans>
测试
package com.test;
import com.hww.bean.Client;
import com.hww.dao.ClientDao;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
@Test
public void test9(){
Client client = new Client();
client.setClientId(1);
ClientDao clientDao = (ClientDao)context.getBean("clientDao");
System.out.println(clientDao.selectByPrimaryKey(client));
}
}
UserInfo
默认对应的表名为user_info
。@Table(name = "tableName")
进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.@Column
一样,都会作为表字段,表字段默认为Java对象的Field
名字驼峰转下划线形式.@Column(name = "fieldName")
指定不符合第3条规则的字段名@Transient
注解可以忽略字段,添加该注解的字段不会作为表字段使用.@Id
注解作为主键的字段,可以有多个@Id
注解的字段作为联合主键.@Id
注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低).tk.mybatis.mapper.model.UserLogin2
类.@NameStyle
注解,用来配置对象名/字段和表名/字段之间的转换方式,该注解优先于全局配置style
,可选值:
normal
:使用实体类名/属性名作为表名/字段名camelhump
:这是默认值,驼峰转换为下划线形式uppercase
:转换为大写lowercase
:转换为小写1. 适用于所有可以自增的数据库
2. 获取主键后可会写
public class Client {
@Id
@GeneratedValue(generator = "JDBC")
private Integer clientId;
}
相当于xml中。的selectKey ,useGeneratedKeys+keyProperty属性
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="client_id">
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>
测试:插入成功后,打印client对象,id是回写的。
@Test
public void test2(){
ClientDao clientDao = (ClientDao)context.getBean("clientDao");
Client client = new Client();
client.setClientName("东皇太一3");
client.setClientNameEn("donghuataiyi3");
clientDao.insert(client);
System.out.println(client);
}
GenerationType
TABLE:使用一个特定的数据库表格来保存主键。
SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
IDENTITY:主键由数据库自动生成(主要是自动增长型)
AUTO:主键由程序控制。
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer clientId;
}
相当于xml
<insert id="insertAuthor">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "select SEQ_ID.nextval from dual")
private Integer id;
相当于xml
<insert id="insertAuthor">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select SEQ_ID.nextval from dual
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
</insert>
注意使用这个时,order属性一定所以before,需要在配置中配置,生成序列一定先执行
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hww.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSession"></property>
<property name="properties">
<value>
order=BEFORE
</value>
</property>
</bean>
当然上面谈到strategy = GenerationType.SEQUENCE 执行序列
这个要和@SequenceGenerator注解配合使用,指定序列名称,如果不指定会自动生成一个序列
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name="client_seq")
private Integer id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "select uuid()")
private Integer id;
执行类
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import sun.nio.cs.Surrogate;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Gen {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(
Surrogate.Generator.class.getResourceAsStream("/generator/generatorConfig.xml"));
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
生成策略配置
<?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>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true -->
<property name="caseSensitive" value="true"/>
</plugin>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"
userId="root"
password="root">
</jdbcConnection>
<javaModelGenerator targetPackage="com.hww.bean" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="com.hww.mapper" targetProject="src/main/resources"/>
<table tableName="t_ecif_client" >
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>