前置工作 導包(mysql-connector-java、mybatis) 實體類 Mapper層 1.介面 public interface BookMapper { public Book getBookById(Integer bookID); } 2.創建Mapper的映射文件 <?xml ...
前置工作
- 導包(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
作者:御靈之靈 轉載請註明原文鏈接:https://www.cnblogs.com/yulingzhiling/p/18099119 若標題中有“轉載”字樣,則本文版權歸原作者所有。若無轉載字樣,本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利.