http://my.oschina.net/vigiles/blog/125127
Mybatis上路_05-使用命令行自动生成
1人收藏此文章,
我要收藏 发表于1个月前(2013-04-24 22:22) , 已有
25次阅读 ,共
0个评论
1.数据准备:
mysql5.5 。
1)建库:
1 |
CREATE DATABASE DB_MYBATIS CHAR SET UTF8;
|
2)建表:
1 |
CREATE TABLE TB_GENERATOR(
|
2 |
id INT PRIMARY KEY AUTO_INCREMENT,
|
3)预设数据:
1 |
INSERT INTO TB_GENERATOR VALUES (1, "eminem1" , "notes1" );
|
2 |
INSERT INTO TB_GENERATOR VALUES (2, "eminem2" , "notes2" );
|
2.编写Generator执行配置文件:
generatorConfigation.xml 。
01 |
<? xml version = "1.0" encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE generatorConfiguration |
03 |
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
|
04 |
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
|
06 |
< generatorConfiguration >
|
08 |
< classPathEntry location = "mysql-connector-java-5.1.22-bin.jar" />
|
10 |
<!-- 环境配置 **************************************************
|
12 |
targetRuntime="MyBatis版本" -->
|
13 |
< context id = "MySqlTables" targetRuntime = "MyBatis3" >
|
15 |
<!-- 数据库 ===============================================
|
17 |
connectionURL="数据库url"
|
21 |
driverClass = "com.mysql.jdbc.Driver"
|
22 |
connectionURL = "jdbc:mysql://127.0.0.1:3306/db_mybatis" |
27 |
<!-- Java数据类型分离器,数据类型转换 =====================
|
32 |
< property name = "forceBigDecimals" value = "false" />
|
35 |
<!-- Java模型生成器 =======================================
|
36 |
targetPackage="生成文件的目标路径"
|
37 |
targetProject="已存在的项目包"
|
38 |
enableSubPackages-可以省略对应约束名,true-是的,可以省略tb_generator
|
39 |
trimString-验证字符串,true-是的 -->
|
41 |
targetPackage = "test.model"
|
42 |
targetProject = "\cn\cvu" >
|
43 |
< property name = "enableSubPackages" value = "false" />
|
44 |
< property name = "trimStrings" value = "true" />
|
47 |
<!-- sql映射文件生成器 =======================================
|
48 |
targetPackage="生成文件的目标路径"
|
49 |
targetProject="已存在的项目包"
|
50 |
enableSubPackages-可以省略对应约束名,true-是的,可以省略tb_generator -->
|
52 |
targetPackage = "test.xml"
|
53 |
targetProject = "\cn\cvu" >
|
54 |
< property name = "enableSubPackages" value = "false" />
|
57 |
<!-- java委托对象DAO生成器,接口 =============================
|
59 |
targetPackage="生成文件的目标路径"
|
60 |
targetProject="已存在的项目包"
|
61 |
enableSubPackages-可以省略对应约束名,true-是的,可以省略tb_generator -->
|
64 |
targetPackage = "test.dao"
|
65 |
targetProject = "\cn\cvu" >
|
66 |
< property name = "enableSubPackages" value = "false" />
|
67 |
</ javaClientGenerator >
|
69 |
<!-- 表单,已存在,根据此表生成其它文件 =======================
|
71 |
domainObjetctName="对应生成的文件名前缀" -->
|
72 |
< table tableName = "tb_generator" domainObjectName = "Eminem" >
|
73 |
<!-- useActualColumnNames-使用真实列名 ,
|
74 |
true-嗯。如果false,则由columnOverride节点控制 -->
|
75 |
< property name = "useActualColumnNames" value = "true" />
|
78 |
column="列名" sqlStatement="某种数据库语言" identity="作为主键" -->
|
79 |
< generatedKey column = "id" sqlStatement = "MySql" identity = "true" />
|
81 |
<!-- 覆盖项,当上面的property name="useActualColumnNames" value="true"时。
|
82 |
column="被覆盖值的列名" property="用来覆盖的对应的java类的属性" -->
|
83 |
< columnOverride column = "name" property = "generName" />
|
88 |
</ generatorConfiguration >
|
3.搭建执行环境:
1)创建项目包:
2)准备文件:
4.执行命令行语句:
C:\> java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfigation.xml
如果增加“ -overwrite”参数会覆盖原有文件。事实上原有文件会被重命名。
5.查看生成的文件:
6.查看文件内容:
在MyEclipse中新建空web项目,将“cn”包放入“src”内。
1) cn/cvu/dao/EminemMapper.java :
(1)修改自动生成的包路径。如果没有注解类包,可以注释图中第4行代码。
(2)然后将图中两行代码中的注解删除。
2)cn/cvu/model/Eminem.java :
修改包路径。
3) cn/cvu/model/EminemExample.java :
修改包路径。
4)cn/cvu/xml/EminemMapper.xml :
修改映射文件路径:
7.测试,使用生成的文件查询:
1)导入MyBatis的jar包:
mybatis-3.2.2.jar
mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.1.22-bin.jar
2)创建MyBatis的xml配置文件:
上面我们注释掉了自动生成的注解代码,在src新建一个xml配置文件,mybatisConfigation.xml:
01 |
<? xml version = "1.0" encoding = "UTF-8" ?>
|
02 |
<!DOCTYPE configuration |
03 |
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
|
04 |
"http://mybatis.org/dtd/mybatis-3-config.dtd">
|
07 |
< environments default = "development" >
|
08 |
< environment id = "development" >
|
09 |
< transactionManager type = "JDBC" />
|
10 |
< dataSource type = "POOLED" >
|
11 |
< property name = "driver" value = "com.mysql.jdbc.Driver" />
|
12 |
< property name = "url" value = "jdbc:mysql://127.0.0.1:3306/db_mybatis" />
|
13 |
< property name = "username" value = "root" />
|
14 |
< property name = "password" value = "root" />
|
21 |
<!-- 引入MBG生成器创建的映射文件 -->
|
22 |
< mapper resource = "cn/cvu/test/xml/EminemMapper.xml" />
|
3)Java测试代码:
创建测试类cn.cvu.test.dotest.TestGenerateWithXML :
01 |
package cn.cvu.test.dotest;
|
03 |
import java.io.Reader;
|
04 |
import java.util.List;
|
05 |
import org.apache.ibatis.io.Resources;
|
06 |
import org.apache.ibatis.session.SqlSession;
|
07 |
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
08 |
import org.junit.Test;
|
09 |
import cn.cvu.test.dao.EminemMapper;
|
10 |
import cn.cvu.test.model.Eminem;
|
11 |
import cn.cvu.test.model.EminemExample;
|
12 |
import cn.cvu.test.model.EminemExample.Criteria;
|
14 |
public class TestGenerateWithXML {
|
17 |
public void testQuery() throws Exception {
|
20 |
Reader reader = Resources.getResourceAsReader( "mybatisConfigation.xml" );
|
21 |
SqlSession session = new SqlSessionFactoryBuilder().build(reader).openSession();
|
24 |
EminemExample eminemExample = new EminemExample();
|
27 |
EminemMapper mapper = session.getMapper(EminemMapper. class );
|
30 |
List<Eminem> eminems = mapper.selectByExample(eminemExample);
|
31 |
System.out.println( "ID \t\t name \t\t notes \n" );
|
32 |
for (Eminem eminem : eminems) {
|
33 |
System.out.println(eminem.getId()+
|
34 |
"\t\t" +eminem.getGenerName()+
|
35 |
"\t\t" +eminem.getNotes());
|
- end