使用Mybatis Generator工具逆向操作Mysql数据库
使用Mybatis Generator工具逆向操作Mysql数据库
(1) 安装Mybatis Generator插件:https://blog.****.net/weixin_42148410/article/details/80258646
(2) 在项目的src结点下创建一个Mybatis的Genertor配置文件(保持默认,单击Finish)
(3) 对生成的genneratorConfig.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="context1">
<!-- 连接数据库 -->
<!-- 在(同一或不同)数据库中有多张表的名称与目标表的名称相同,如果使用Connector/J版本8.x, 您可能会注意到生成器试图为MySQL信息模式(sys、Information_schema、Performanceschema等)中的表生成代码。
这可能不是你想要的!若要禁用此行为,请将属性“nullCatalogMeansCurrent=true”添加到您的JDBCURL中。 -->
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/rsgl"
driverClass="com.mysql.cj.jdbc.Driver" password="ansft572" userId="root">
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<!-- 生成的文件的目标地址 -->
<javaModelGenerator targetPackage="sqlmapping"
targetProject="mybatis_mysql" />
<sqlMapGenerator targetPackage="sqlmapping"
targetProject="mybatis_mysql" />
<javaClientGenerator targetPackage="sqlmapping"
targetProject="mybatis_mysql" type="XMLMAPPER" />
<!-- 逆向的表 ,schema指定数据库,不然其他数据库有同名的表会出错 -->
<table schema="rsgl" tableName="userinfo">
</table>
</context>
</generatorConfiguration>
通过配置文件genneratorConfig.xml可以将数据表的结构逆向出对象的java类和存储SQL语句的SQL映射文件,然后用Mybatis的API就可以对这些java类进行操作。从而演变成对数据表的增删改查操作。
(4) 添加JDBC驱动,还有添加Mybatis的jar包。在MyEclipse中直接把这些jar包添配置到项目就可以了。
右键单击genneratorConfig.xml文件,在Run As中添加JDBC驱动,选择Run Configurations。按(1),(2),(3),(4)步骤将驱动包添加到classpath中。
(5) 单击右下角Run按钮开始逆向。
(6) 在SRC中创建的mybatis-config,xml配置文件。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置Mybatis多套运行环境 -->
<environments default="development">
<environment id="development">
<!-- 配置书屋管理器,采用JDBC的事务管理器 -->
<transactionManager type="JDBC" />
<!-- POOLED:Mybatis自带的数据源,JNDI:基于tomcat的数据源 -->
<dataSource type="POOLED">
<!-- 数据库驱动类 -->
<property
name="driver"
value="com.mysql.cj.jdbc.Driver" />
<!-- 连接数据库的url -->
<property
name="url"
value="jdbc:mysql://localhost:3306/rsgl" />
<!-- 数据库用户名和密码 -->
<property
name="username"
value="root" />
<property
name="password"
value="ansft572" />
</dataSource>
</environment>
</environments>
<!-- 具体指定SQL映射文件的路径 -->
<mappers>
<mapper resource="sqlmapping/UserinfoMapper.xml" />
</mappers>
</configuration>
(7) 测试类
向表中添加一条数据
public class Test {
public static void main(String[] args) {
try {
Userinfo userinfo = new Userinfo();
userinfo.setUsername("chen");
userinfo.setPassword("abc123");
userinfo.setAge(888);
String resource = "mybatis-config.xml";
InputStream inputStream;
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("insert", userinfo);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}