Mybatis 逆向工程
1、概念
mybatis可以根据已有的数据库表进行相应的代码生成,为项目的创建书写提供便利,会根据数据库表字段创建对应的实体类,与通用mapper接口以及对应的实现配置,额外提供了对于单表操作动态拼写的相应配置.
2、逆向工程创建
(1)创建项目
(2)导入逆向工程jar包与mysql连接jar包
所需资源链接:https://pan.baidu.com/s/1OsSVCfRMnWcFzRgJC4J54A
提取码:3kem
(3)编写生成配置文件(gen.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/mydb" userId="root"
password="root">
</jdbcConnection>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.yunhe.pojo"
targetProject="./src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.yunhe.mapper"
targetProject="./src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.yunhe.mapper"
targetProject="./src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="author"></table>
<table tableName="commodity"></table>
<table tableName="order"></table>
<table tableName="role"></table>
<table tableName="user"></table>
</context>
</generatorConfiguration>
(4)创建类读取配置文件生成代码(Test类)
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 java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//加载逆向生成配置文件信息
//加载src下的配置文件
InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream("gen.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
//如果加载绝对路径下的配置文件可以将resourceAsStream改为new File(url);
Configuration config = cp.parseConfiguration(resourceAsStream);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
3 生成代码基本使用
mybatis生成实体类只会生成对应的属性以及gettter与setter方法,构造方法、序列化、toString方法根据实际使用添加
3.1 添加
SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//添加
User user=new User("新增","1234576");
//insert into user (uid, uusername, upassword ) values (?, ?, ? )
mapper.insert(user);
//insert into user ( uusername, upassword ) values ( ?, ? )
mapper.insertSelective(user);
DEBUG [main] - ==> Preparing: insert into user (uid, uusername, upassword ) values (?, ?, ? )
DEBUG [main] - ==> Parameters: null, 新增(String), 1234576(String)
DEBUG [main] - <== Updates: 1
DEBUG [main] - ==> Preparing: insert into user ( uusername, upassword ) values ( ?, ? )
DEBUG [main] - ==> Parameters: 新增(String), 1234576(String)
DEBUG [main] - <== Updates: 1
3.2 修改
SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//修改
User user=new User(8,null,"1234576");
//update user set uusername = ?, upassword = ? where uid = ?
mapper.updateByPrimaryKey(user);
//update user SET upassword = ? where uid = ?
mapper.updateByPrimaryKeySelective(user);
DEBUG [main] - ==> Preparing: update user set uusername = ?, upassword = ? where uid = ?
DEBUG [main] - ==> Parameters: null, 1234576(String), 8(Integer)
DEBUG [main] - <== Updates: 1
DEBUG [main] - ==> Preparing: update user SET upassword = ? where uid = ?
DEBUG [main] - ==> Parameters: 1234576(String), 8(Integer)
DEBUG [main] - <== Updates: 1
3.3 删除
SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//删除
//delete from user where uid = ?
mapper.deleteByPrimaryKey(8);
DEBUG [main] - ==> Preparing: delete from user where uid = ?
DEBUG [main] - ==> Parameters: 8(Integer)
DEBUG [main] - <== Updates: 1
3.4 查询
SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//查询
//select uid, uusername, upassword from user where uid = ?
User user = mapper.selectByPrimaryKey(1);
System.out.println(user);
DEBUG [main] - ==> Preparing: select uid, uusername, upassword from user where uid = ?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
User{uid=1, uusername='zhangsan', upassword='sadsadsad'}