前置工作
- 導包(mysql-connector-java、mybatis)
- 實體類
Mapper層
1.介面
public interface BookMapper {
public Book getBookById(Integer bookID);
}
2.建立Mapper的對映檔案
<?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="com.ylzl.mapper.BookMapper">
<select id="getBookById" resultType="com.ylzl.pojo.Book">
select * from books where bookID=#{bookID}
</select>
</mapper>
Service層
可以不使用Service層,使用其他方式包含以下核心程式碼也可以。
核心程式碼
static {
String resource = "mybatis-config.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
//1.讀取xml配置檔案,建立SqlSessionFactory物件
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//2.透過建立SqlSessionFactory物件獲取SqlSession物件
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.透過SqlSession物件獲取代理物件mapper,傳入介面class物件
BookMapper mapper=sqlSession.getMapper(BookMapper.class);
//4.透過mapper呼叫介面對應方法
mapper.getBookById(bookID);
以下為使用Service層實現的方式
1.介面
public interface BookService {
Book getBookById(Integer bookID);
}
2.介面實現類(重點關注)
public class BookServiceImpl implements BookService{
static InputStream inputStream = null;
static {
String resource = "mybatis-config.xml";
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
//1.讀取xml配置檔案,建立SqlSessionFactory物件
private final SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(inputStream);
@Override
public Book getBookById(Integer bookID) {
//2.透過建立SqlSessionFactory物件獲取SqlSession物件
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.透過SqlSession物件獲取代理物件mapper,傳入介面class物件,
BookMapper mapper = sqlSession.getMapper(BookMapper.class);
//4.透過mapper呼叫介面對應方法
return mapper.getBookById(bookID);
}
}
Mybatis配置檔案mybatis-config.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>
<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:3306/ssmbuild"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--配置對映檔案-->
<mappers>
<mapper resource="com/ylzl/mapper/BookMapper.xml"/>
</mappers>
</configuration>
測試
BookService bookServicelmpl = new BookServiceImpl();
System.out.println(bookServicelmpl.getBookById(2));
遇到的問題
1.BookMapper.xml不放在resources下,加過濾
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2.解決xml編碼問題:1 位元組的 UTF-8 序列的位元組 1 無效:https://www.cnblogs.com/thetree/p/12991403.html