Mybatis总结
Posted 三碗才能饱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis总结相关的知识,希望对你有一定的参考价值。
jdbc.properties
1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://127.0.0.1:3306/testdb
3 username=root
4 password=wjb
log4j.properties
1 log4j.rootLogger=DEBUG,CONSOLE,file
2 #log4j.rootLogger=ERROR,ROLLING_FILE
3 log4j.logger.cn.jbit.dao=debug
4 log4j.logger.com.ibatis=debug
5 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
6 log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
7 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
8 log4j.logger.java.sql.Connection=debug
9 log4j.logger.java.sql.Statement=debug
10 log4j.logger.java.sql.PreparedStatement=debug
11 log4j.logger.java.sql.ResultSet=debug
12 log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug
13
14 ######################################################################################
15 # Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
16 ######################################################################################
17 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
18 log4j.appender.Threshold=error
19 log4j.appender.CONSOLE.Target=System.out
20 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
21 log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n
22
23
24 ######################################################################################
25 # DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
26 ######################################################################################
27 log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
28 log4j.appender.file.DatePattern=yyyy-MM-dd
29 log4j.appender.file.File=log.log
30 log4j.appender.file.Append=true
31 log4j.appender.file.Threshold=error
32 log4j.appender.file.layout=org.apache.log4j.PatternLayout
33 log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
34
35
36 log4j.logger.com.opensymphony.xwork2=error
mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
4
5 <configuration>
6 <properties resource="jdbc.properties">
7
8 </properties>
9 <environments default="development">
10 <environment id="development">
11 <transactionManager type="JDBC"/>
12 <dataSource type="POOLED">
13 <property name="driver" value="${driver}"/>
14 <property name="url" value="${url}"/>
15 <property name="username" value="${username}"/>
16 <property name="password" value="${password}"/>
17 </dataSource>
18 </environment>
19 </environments>
20
21 <mappers>
22 <mapper resource="dao/UserMapper.xml"/>
23 </mappers>
24 </configuration>
UserMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4
5 <mapper namespace="dao.UserMapper">
6 <select id="count" resultType="int">
7 select count(1) from user
8 </select>
9
10 </mapper>
UserTest
1 package test;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7
8 import org.apache.ibatis.io.Resources;
9 import org.apache.ibatis.session.SqlSession;
10 import org.apache.ibatis.session.SqlSessionFactory;
11 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
12 import org.apache.log4j.Logger;
13 import org.junit.Test;
14
15 public class UserTest {
16 private Logger logger = Logger.getLogger(UserTest.class);
17
18 @Test
19 public void countTest() {
20 String resource = "mybatis-config.xml";
21 int count = 0;
22 SqlSession sqlSession = null;
23 //1,读取配置文件mybatis-config.xml输入流
24 try {
25 InputStream is = Resources.getResourceAsStream(resource);
26 //2,创建sqlsessionfactory
27 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
28 //3,创建sqlsession
29 sqlSession = factory.openSession();
30 //4,调用mapper配置文件
31 count = sqlSession.selectOne("dao.UserMapper.count");
32
33 logger.debug(count);
34 } catch (IOException e) {
35 e.printStackTrace();
36 }finally{
37 sqlSession.close();
38 }
39
40
41
42 }
43
44 }
以上是关于Mybatis总结的主要内容,如果未能解决你的问题,请参考以下文章
markdown [mybatis参考]关于mybatis #mybatis的一些片段