4.Improve SqlSession

 

工具类: SqlSessionUtil

SqlSessionUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionUtil {

private static ThreadLocal<SqlSession> local = new ThreadLocal<SqlSession>();
private static SqlSessionFactory sf = null;

static {
InputStream inputStream = null;

try {
inputStream = Resources.getResourceAsStream("mybatis.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

sf = new SqlSessionFactoryBuilder().build(inputStream);
}

public static SqlSession getSession() {
SqlSession session = local.get();
if(session == null) {
session = sf.openSession();
local.set(session);
}
return session;
}

public static void close(SqlSession session) {
if(session != null) {
session.close();
local.remove();
}
}
}

类: StudentDaoImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class StudentDaoImpl implements StudentDao{

@Override
public void insertStudent(Student student) {
SqlSession session=SqlSessionUtil.getSession();
session.insert("insertStudent", student);
session.commit();
SqlSessionUtil.close(session);
}

}

添加属性文件: jdbc.properties

jdbc.properties

1
2
3
4
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=mysql

mybatis.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>
<!-- 引入属性文件 -->
<properties resource="jdbc.properties"></properties>

<environments default="mysql">
<environment id="mysql">
...
</environment>
</environments>
<mappers>
<mapper resource="com/node/dao/StudentDao.xml"/>
</mappers>
</configuration>

使用日志文件输出SQL语句

log4j.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
# Copyright 2009-2016 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

### Global logging configuration
log4j.rootLogger=ERROR, stdout
### Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

log4j.logger.test=trace,console

log4j.logger.[namespace]=trace,console

StudentDao.xml

1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test">
...
</mapper>