Java如何创建MyBatis SqlSession对象?
要创建一个,SqlSession您可以使用SqlSessionFactoryclassopenSession()方法。此方法提供了一些可以配置会话属性的重载方法。例如,我们可以配置会话的自动提交模式和事务隔离级别。
package org.nhooo.example.mybatis;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.nhooo.example.mybatis.domain.Record;
import java.io.IOException;
import java.io.Reader;
public class SqlSessionDemo {
public static void main(String[] args) throws IOException {
SqlSessionDemo demo = new SqlSessionDemo();
//建立一个SqlSessionFactory。
SqlSessionFactory factory = demo.getSessionFactory();
//通过使用factory.openSession()方法创建一个SqlSession。
//在以下情况下,使用try-with-resources块是一个好习惯
//使用SqlSession。会话将自动
//完成工作后关闭。
try (SqlSession session = factory.openSession()) {
Record record = session.selectOne("getRecord", 1L);
System.out.println("Record = " + record);
}
}
/**
* Build an SqlSessionFactory.
*
* @return an SqlSessionFactory.
* @throws IOException when fail to read the configuration file.
*/
private SqlSessionFactory getSessionFactory() throws IOException {
Reader reader = Resources.getResourceAsReader("configuration.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
return builder.build(reader);
}
}以下是配置,映射器和POJO。
configuration.xml
<?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>
<typeAliases>
<typeAlias alias="record" type="org.nhooo.example.mybatis.domain.Record"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/musicdb"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/nhooo/example/mybatis/mapper/RecordMapper.xml"/>
</mappers>
</configuration>RecordMapper.xml
<?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="org.nhooo.example.mybatis.persistence.RecordMapper">
<resultMap id="recordResultMap" type="record">
<result column="release_date" property="releaseDate"/>
<result column="artist_id" property="artistId"/>
<result column="label_id" property="labelId"/>
</resultMap>
<select id="getRecord" parameterType="java.lang.Long" resultMap="recordResultMap">
SELECT
id,
title,
release_date,
artist_id,
label_id
FROM records
WHERE id = #{id}
</select>
</mapper>Record.java
package org.nhooo.example.mybatis.domain;
import java.io.Serializable;
import java.util.Date;
public class Record implements Serializable {
private Long id;
private String title;
private Date releaseDate;
private Long artistId;
private Long labelId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public Long getArtistId() {
return artistId;
}
public void setArtistId(Long artistId) {
this.artistId = artistId;
}
public Long getLabelId() {
return labelId;
}
public void setLabelId(Long labelId) {
this.labelId = labelId;
}
@Override
public String toString() {
return "Record{" +
"id=" + id +
", title='" + title + '\'' +
", releaseDate=" + releaseDate +
", artistId=" + artistId +
", labelId=" + labelId +
'}';
}
}上面的代码的目录结构是:
├── pom.xml
└── src
└── main
├── java
│ └── org
│ └── nhooo
│ └── example
│ └── mybatis
│ ├── SqlSessionDemo.java
│ └── domain
│ └── Record.java
└── resources
├── configuration.xml
└── org
└── nhooo
└── example
└── mybatis
└── mapper
└── RecordMapper.xmlMaven依赖
<!-- https://search.maven.org/remotecontent?filepath=org/mybatis/mybatis/3.5.0/mybatis-3.5.0.jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>