什么是逆向工程
mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po…)
配置文件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="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
password="mysql">
</jdbcConnection>
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection> -->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="cn.itcast.ssm.po"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="cn.itcast.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.itcast.ssm.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="items"></table>
<table tableName="orders"></table>
<table tableName="orderdetail"></table>
<table tableName="user"></table>
</context>
</generatorConfiguration>
执行生成程序
public class GeneratorSqlMapDemo01 {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
}
根据数据库一张表完成逆向工程的例子
逆向工程生成的student类
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
private String email;
.............
对逆向工程生成的StudentMapper建立测试类进行测试
/**
* 凡是带selective的方法的特点:遇到null字段忽略。
*
* @author h
*
*/
public class StudentMapperTest {
private StudentMapper studentMapper;
@Before
public void setUp() throws Exception {
SqlSession sqlSession=MyBatisUtils.getSqlSession();
this.studentMapper=sqlSession.getMapper(StudentMapper.class);
System.out.println("this.studentMapper="+this.studentMapper);
}
@Test
public void testCountByExample() {
StudentExample studentExample=new StudentExample();
studentExample.setDistinct(true);
studentExample.setOrderByClause("id desc");
int count=this.studentMapper.countByExample(studentExample);
System.out.println("count="+count);
}
@Test
public void testDeleteByExample() {
StudentExample example=new StudentExample();
Criteria criteria=example.createCriteria();
criteria.andNameEqualTo("小丽");
int count=this.studentMapper.deleteByExample(example);
System.out.println("count="+count);
}
@Test
public void testDeleteByPrimaryKey() {
int id=10;
int count=this.studentMapper.deleteByPrimaryKey(id);
System.out.println("count="+count);
}
@Test
public void testInsert() {
Student student=new Student();
student.setName("小娥");
int count=this.studentMapper.insert(student);
System.out.println("count="+count);
}
@Test
public void testInsertSelective() {
Student student=new Student();
student.setName("小静");
int count=this.studentMapper.insertSelective(student);
System.out.println("count="+count);
}
@Test
public void testSelectByExample() {
StudentExample example=new StudentExample();
Criteria criteria=example.createCriteria();
String condition="小";
criteria.andNameLike("%"+condition+"%");
criteria.andSexEqualTo("男");
List<Student> list=this.studentMapper.selectByExample(example);
System.out.println(list);
}
@Test
public void testSelectByPrimaryKey() {
int id=1;
Student student=this.studentMapper.selectByPrimaryKey(id);
System.out.println(student);
}
@Test
public void testUpdateByExampleSelective() {
int id=8;
Student student=this.studentMapper.selectByPrimaryKey(id);
if(student!=null){
student.setName("丽儿");
student.setAge(18);
int count=this.studentMapper.updateByPrimaryKeySelective(student);
System.out.println("count="+count);
}else{
System.out.println("学生不存在!");
}
}
@Test
public void testUpdateByExample() {
int id=8;
Student student=this.studentMapper.selectByPrimaryKey(id);
if(student!=null){
StudentExample studentExample=new StudentExample();
Criteria criteria=studentExample.createCriteria();
criteria.andNameEqualTo("丽儿");
student.setName("妹儿");
student.setAge(18);
int count=this.studentMapper.updateByExample(student, studentExample);
System.out.println("count="+count);
}else{
System.out.println("学生不存在!");
}
}
@Test
public void testUpdateByPrimaryKeySelective() {
int id=8;
Student student=this.studentMapper.selectByPrimaryKey(id);
if(student!=null){
student.setName("小美");
int count=this.studentMapper.updateByPrimaryKeySelective(student);
System.out.println("count="+count);
}
}
@Test
public void testUpdateByPrimaryKey() {
int id=8;
Student student=this.studentMapper.selectByPrimaryKey(id);
if(student!=null){
student.setName("妹儿");
int count=this.studentMapper.updateByPrimaryKey(student);
System.out.println("count="+count);
}
}
}