<?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>
<classPathEntry
location="./lib/mysql-connector-java-5.1.26-bin.jar" />
<context id="mybatisDemoForMysql" targetRuntime="MyBatis3">
<!-- 控制注释 -->
<commentGenerator>
<!-- 是否去除所有自动生成的注释文件 -->
<property name="suppressAllComments" value="true" />
<!-- 是否去除所有自动生成的文件的时间戳,默认为false -->
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 控制数据库 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mybatis_db?characterEncoding=utf8"
userId="root" password="david0110" />
<javaTypeResolver>
<!-- 把jdbc中的decimal与numberic类型转化为integer类型 -->
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 数据库表对应的model -->
<javaModelGenerator targetPackage="david.model"
targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 控制Model的xmlMapper文件 -->
<sqlMapGenerator targetPackage="david.mappers"
targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 控制mapper接口 -->
<javaClientGenerator targetPackage="david.inter"
type="XMLMAPPER" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="methodNameCalculator" value="extended" />
</javaClientGenerator>
<!-- schema你的数据库,tableName表明,domainObjectName对应你的javabean类名,是否生成相应的example -->
<table schema="mybatis_db" tableName="visitor" domainObjectName="Visitor"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<generatedKey column="id" sqlStatement="MySql" />
<columnOverride column="name" property="visitor_name" />
<ignoreColumn column="status" delimitedColumnName="false" />
</table>
</context>
</generatorConfiguration>
private static void generateMbgConfiguration() {
/*
* Mybatis自带Generator工具生成相应东西
*/
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("./src/david/mbg/mbg_configuration.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XMLParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
try {
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (InvalidConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("生成Mybatis配置成功!");
}
/*
* 查询访问者信息
*/
public static void testGenerateAdd() {
SqlSession session = MybatisUtils.getSqlSession();
VisitorMapper vOperation = session.getMapper(VisitorMapper.class);
Visitor visitor = new Visitor();
visitor.setVisitor_name("hello2");
visitor.setEmail("helloworld2@qq.com");
visitor.setCreatetime(new Date());
int count = vOperation.insert(visitor);
session.commit();
MybatisUtils.closeSession(session);
MybatisUtils.showMessages(CRUD_Enum.Add, count);
}
/*
* 查询访问者信息
*/
public static void testGenerateQuery(int id) {
SqlSession session = MybatisUtils.getSqlSession();
VisitorMapper vOperation = session.getMapper(VisitorMapper.class);
Visitor visitor = vOperation.selectByPrimaryKey(id);
MybatisUtils.closeSession(session);
MybatisUtils.showMessages(CRUD_Enum.Query, 1);
System.out.println(visitor);
}
public static void testGenerateDelete(int id) {
SqlSession session = MybatisUtils.getSqlSession();
VisitorMapper vOperation = session.getMapper(VisitorMapper.class);
int count = vOperation.deleteByPrimaryKey(id);
session.commit();
MybatisUtils.closeSession(session);
MybatisUtils.showMessages(CRUD_Enum.Delete, count);
}
public static void testGenerateUpdate(int id) {
SqlSession session = MybatisUtils.getSqlSession();
VisitorMapper vOperation = session.getMapper(VisitorMapper.class);
Visitor visitor = vOperation.selectByPrimaryKey(id);
System.out.println(visitor);
String name = visitor.getVisitor_name();
if (name.contains("update")) {
visitor.setVisitor_name(name.substring(0, name.indexOf("update")));
} else {
visitor.setVisitor_name(name + "update");
}
int count = vOperation.updateByPrimaryKey(visitor);
session.commit();
MybatisUtils.closeSession(session);
MybatisUtils.showMessages(CRUD_Enum.Update, count);
System.out.println(visitor);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有