1、添加依赖
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</plugin>
2、添加config.xml
在resources目录下创建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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/demo"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper"
targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="country" domainObjectName="Country"></table>
</context>
</generatorConfiguration>
<table>标签添加数据表和对应的实体类名
然后修改xml中的项目包名,jdbcConnection
3、执行命令
mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate
此命令会覆盖原有Java文件(每次覆盖会在mapper.xml中double<resultmap>,<sql>,crud.注意删除)
4、application.propertity
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.example.demo.model
mybatis.mapper-locations=classpath:mapper/*.xml
5、启动类添加
@MapperScan(basePackages = "com.example.demo.mapper")
如何添加联合查询
1、在SchoolMapper中添加接口
添加的接口为:
List<School> selectByExampleWithCountry(SchoolExample example);//用example查询出带国家信息的学校信息
School selectByPrimaryKeyWithCountry(Integer schoolId);//用primaryKey查询出带国家信息的学校信息
2、在school类中添加Country类型属性和构造方法
3、修改SchoolMapper.xml
<resultMap id="WithCountryResultMap" type="com.example.demo.model.School">
<id column="school_id" jdbcType="INTEGER" property="schoolId" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="country_id" jdbcType="INTEGER" property="countryId" />
<result column="admin_id" jdbcType="INTEGER" property="adminId" />
<!--指定联合查询出的国家字段的封装-->
<association property="country" javaType="com.example.demo.model.Country">
<id column="country_id" property="countryId"/>
<result column="name" property="name"/>
</association>
</resultMap>
4、添加<sql>和<select>
<sql id="WithCountry_Column_List">
s.school_id, s.name, s.coutry_id, s.admin_id,c.country_id, c.name
</sql>
<!--查询学校同时带国家信息-->
<select id="selectByExampleWithCountry" resultMap="WithCountryResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="WithCountry_Column_List" />
from school s
left join country c on s.country_id = c.country_id
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
5、同理添加selectByExampleWithCountry
<!--查询学校并不带国家信息-->
<select id="selectByExample" parameterType="com.example.demo.model.SchoolExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from school
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
6、单元测试
在schoolmapper类中按CTRL+shift+T,创建测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SchoolMapperTest {
/**
* 测试SchoolMapper
*/
@Autowired
SchoolMapper schoolMapper;//报错无影响
@Test
public void testCRUD(){
System.out.println(schoolMapper);
//1、插入几个学校
schoolMapper.insertSelective(new School(null,"test1",2,2));
}
}