Skip to content

Commit dfdd247

Browse files
committed
MyBatis3使用示例
1 parent ada1a15 commit dfdd247

13 files changed

Lines changed: 481 additions & 4 deletions

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
## 数据库
1414
* [Apache DbUtils - JDBC Utility Component](src/cn/aofeng/demo/dbutils)
1515
* [JDBC元数据使用示例](src/cn/aofeng/demo/jdbc/MetaDataExample.java)
16+
* [MyBatis3使用示例](src/cn/aofeng/demo/mybatis)
1617

1718
## XML
1819
* [XPath](src/cn/aofeng/demo/xml/XPathDemo.java)

conf/gneratorConfig.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
3+
<generatorConfiguration>
4+
<context id="context1">
5+
<commentGenerator>
6+
<!-- 指定是否自动生成注释 true:不生成, false:生成 -->
7+
<property name="suppressAllComments" value="true"/>
8+
<!-- 是否用表和列的注释生成实体类和字段的注释。注:如果suppressAllComments设置为true,则忽略此配置选项 -->
9+
<property name="addRemarkComments" value="true"/>
10+
</commentGenerator>
11+
12+
<jdbcConnection connectionURL="jdbc:mysql://192.168.56.102:19816/ucgc_monit?useUnicode=true&amp;characterEncoding=UTF8"
13+
driverClass="com.mysql.jdbc.Driver"
14+
userId="uzone"
15+
password="uzone">
16+
</jdbcConnection>
17+
18+
<!-- 存放实体类的package -->
19+
<javaModelGenerator targetPackage="cn.aofeng.demo.mybatis.entity"
20+
targetProject="JavaTutorial/src" />
21+
<!-- 存放SQL语句和结构映射配置文件的package -->
22+
<sqlMapGenerator targetPackage="cn.aofeng.demo.mybatis.mapper"
23+
targetProject="JavaTutorial/src" />
24+
<!-- 存放DAO类的package -->
25+
<javaClientGenerator targetPackage="cn.aofeng.demo.mybatis.dao"
26+
targetProject="JavaTutorial/src" type="XMLMAPPER" />
27+
28+
<table schema="ucgc_monit" tableName="monit_contact"
29+
modelType="flat"
30+
enableCountByExample="false"
31+
enableDeleteByExample="false"
32+
enableSelectByExample="false"
33+
enableUpdateByExample="false"></table>
34+
<table schema="ucgc_monit" tableName="monit_item"
35+
modelType="flat"
36+
enableCountByExample="false"
37+
enableDeleteByExample="false"
38+
enableSelectByExample="false"
39+
enableUpdateByExample="false"></table>
40+
<table schema="ucgc_monit" tableName="monit_notify_history"
41+
modelType="flat"
42+
enableCountByExample="false"
43+
enableDeleteByExample="false"
44+
enableSelectByExample="false"
45+
enableUpdateByExample="false"></table>
46+
<table schema="ucgc_monit" tableName="monit_notify_item"
47+
modelType="flat"
48+
enableCountByExample="false"
49+
enableDeleteByExample="false"
50+
enableSelectByExample="false"
51+
enableUpdateByExample="false"></table>
52+
<table schema="ucgc_monit" tableName="monit_stat"
53+
modelType="flat"
54+
enableCountByExample="false"
55+
enableDeleteByExample="false"
56+
enableSelectByExample="false"
57+
enableUpdateByExample="false"></table>
58+
<table schema="ucgc_monit" tableName="monit_threshold"
59+
modelType="flat"
60+
enableCountByExample="false"
61+
enableDeleteByExample="false"
62+
enableSelectByExample="false"
63+
enableUpdateByExample="false"></table>
64+
</context>
65+
</generatorConfiguration>

conf/log4j.properties

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
log4j.rootLogger=INFO, console
22

3+
# 应用本身
34
log4j.logger.cn.aofeng.demo=DEBUG, console
45
log4j.additivity.cn.aofeng.demo=false
5-
log4j.logger.org.jboss.netty.handler.logging=DEBUG, console
6+
7+
# Netty
8+
log4j.logger.org.jboss.netty.handler.logging=INFO, console
69
log4j.additivity.org.jboss.netty.handler.logging=false
710

8-
# 控制台Console
9-
log4j.additivity.console=false
11+
# MyBatis
12+
log4j.logger.org.apache.ibatis=INFO, console
13+
log4j.additivity.org.apache.ibatis=false
14+
15+
# 控制台输出
1016
log4j.appender.console=org.apache.log4j.ConsoleAppender
1117
log4j.appender.console.layout=org.apache.log4j.PatternLayout
12-
log4j.appender.console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] %p ~ %m%n
18+
log4j.appender.console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] %p %m%n

conf/mybatis-config.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration
3+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
5+
<configuration>
6+
<environments default="development">
7+
<environment id="development">
8+
<transactionManager type="JDBC"/>
9+
<dataSource type="POOLED">
10+
<property name="driver" value="com.mysql.jdbc.Driver"/>
11+
<property name="url" value="jdbc:mysql://192.168.56.102:19816/ucgc_monit?useUnicode=true&amp;characterEncoding=UTF8"/>
12+
<property name="username" value="uzone"/>
13+
<property name="password" value="uzone"/>
14+
</dataSource>
15+
</environment>
16+
</environments>
17+
<mappers>
18+
<mapper resource="cn/aofeng/demo/mybatis/mapper/MonitNotifyHistoryMapper.xml"/>
19+
</mappers>
20+
</configuration>

lib/mybatis-3.4.4.jar

1.53 MB
Binary file not shown.
-813 KB
Binary file not shown.
968 KB
Binary file not shown.

lib/slf4j-api-1.7.21.jar

40.1 KB
Binary file not shown.

lib/slf4j-log4j12-1.7.21.jar

9.75 KB
Binary file not shown.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cn.aofeng.demo.mybatis;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import org.apache.ibatis.io.Resources;
7+
import org.apache.ibatis.session.SqlSessionFactory;
8+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import cn.aofeng.demo.mybatis.dao.MonitNotifyHistoryDao;
13+
import cn.aofeng.demo.mybatis.entity.MonitNotifyHistory;
14+
15+
/**
16+
*
17+
*
18+
* @author <a href="mailto:[email protected]">聂勇</a>
19+
*/
20+
public class MyBatisClient {
21+
22+
private static Logger _logger = LoggerFactory.getLogger(MyBatisClient.class);
23+
24+
public static void main(String[] args) throws IOException {
25+
String resource = "mybatis-config.xml";
26+
InputStream ins = Resources.getResourceAsStream(resource);
27+
SqlSessionFactory ssFactory = new SqlSessionFactoryBuilder().build(ins);
28+
MonitNotifyHistoryDao dao = new MonitNotifyHistoryDao(ssFactory);
29+
30+
// 插入一条记录
31+
long recordId = 999;
32+
MonitNotifyHistory record = new MonitNotifyHistory();
33+
record.setRecordId(recordId);
34+
record.setMonitId(9999);
35+
record.setAppId(99);
36+
record.setNotifyTarget(1);
37+
record.setNotifyType(1);
38+
record.setNotifyContent("通知内容测试");
39+
record.setStatus(0);
40+
record.setCreateTime(1492061400000L);
41+
int count = dao.insert(record);
42+
_logger.info("insert complete, effects {} rows", count);
43+
44+
// 查询记录
45+
MonitNotifyHistory entity = dao.selectByPrimaryKey(recordId);
46+
_logger.info("query record id {}, result: {}", recordId, entity);
47+
48+
// 更新一个字段再执行查询
49+
entity.setRetryTimes(99);
50+
count = dao.updateByPrimaryKeySelective(entity);
51+
_logger.info("update complete, record id:{}, effects {} rows", recordId, count);
52+
entity = dao.selectByPrimaryKey(recordId);
53+
_logger.info("query record id {}, result: {}", recordId, entity);
54+
55+
// 删除记录后再执行查询
56+
count = dao.deleteByPrimaryKey(recordId);
57+
_logger.info("delete complete, record id {}, effects {} rows", recordId, count);
58+
entity = dao.selectByPrimaryKey(recordId);
59+
_logger.info("query record id {}, result: {}", recordId, entity);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)