mybatis框架之mybatis中dao層開(kāi)發(fā)的兩種方法
一、原始dao的開(kāi)發(fā)方式
即開(kāi)發(fā)dao接口和dao實(shí)現(xiàn)類(lèi)。
首先添加Dao接口
public interface UserDao {
// 1、 根據(jù)用戶(hù)ID查詢(xún)用戶(hù)信息
public User findUserById(int id) throws Exception;
?
// 2、 根據(jù)用戶(hù)名稱(chēng)模糊查詢(xún)用戶(hù)列表
public List<User> findUsersByName(String name) throws Exception;
?
// 3、 添加用戶(hù)
public void insertUser(User user) throws Exception;
}然后實(shí)現(xiàn)其接口即可
public class UserDaoImpl implements UserDao {
// 依賴(lài)注入,將工程在外面創(chuàng)建
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {//將外面創(chuàng)建的工廠(chǎng)傳遞進(jìn)來(lái)(以后spring)
this.sqlSessionFactory = sqlSessionFactory;
}
?
@Override
public User findUserById(int id) throws Exception {
// 創(chuàng)建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 調(diào)用SqlSession的增刪改查方法
// 第一個(gè)參數(shù):表示statement的唯一標(biāo)示
User user = sqlSession.selectOne("test.findUserById", id);
System.out.println(user);
// 關(guān)閉資源
sqlSession.close();
return user;
}
?
@Override
public List<User> findUsersByName(String name) {
// 創(chuàng)建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
?
// 調(diào)用SqlSession的增刪改查方法
// 第一個(gè)參數(shù):表示statement的唯一標(biāo)示
List<User> list = sqlSession.selectOne("test.findUsersByName", name);
System.out.println(list);
// 關(guān)閉資源
sqlSession.close();
return list;
}
?
@Override
public void insertUser(User user) {
// 創(chuàng)建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
?
// 調(diào)用SqlSession的增刪改查方法
// 第一個(gè)參數(shù):表示statement的唯一標(biāo)示
sqlSession.insert("test.insertUser", user);
?
System.out.println(user.getId());
// 提交事務(wù)
sqlSession.commit();
// 關(guān)閉資源
sqlSession.close();
}
}那么在測(cè)試類(lèi)中
public class UserDaoTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws Exception {
// 讀取配置文件
// 全局配置文件的路徑
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 創(chuàng)建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindUserById() throws Exception {
// 創(chuàng)建UserDao
UserDao dao = new UserDaoImpl(sqlSessionFactory);
User user = dao.findUserById(1);
System.out.println(user);
}
}二、Mapper代理的開(kāi)發(fā)方式
分析上面的代碼會(huì)發(fā)現(xiàn)有大量的重復(fù)的模板代碼,并且存在硬編碼【如sqlSession.insert("test.insertUser", user);】,
為了解決以上問(wèn)題,故采用開(kāi)發(fā)mapper接口(相當(dāng)于dao接口)來(lái)進(jìn)行dao的開(kāi)發(fā),即通過(guò)開(kāi)發(fā)mapper接口,將自動(dòng)生成其代理類(lèi)來(lái)進(jìn)行操作。
其中Mapper代理使用的是jdk的代理策略。
如果采用Mapper代理的方式開(kāi)發(fā)需要滿(mǎn)足如下開(kāi)發(fā)規(guī)范
- mapper接口的全限定名要和mapper映射文件的namespace值一致。
- mapper接口的方法名稱(chēng)要和mapper映射文件的statement的id一致。
- mapper接口的方法參數(shù)類(lèi)型要和mapper映射文件的statement的parameterType的值一致,而且它的參數(shù)是一個(gè)。
- mapper接口的方法返回值類(lèi)型要和mapper映射文件的statement的resultType的值一致。
首先開(kāi)發(fā)mapper接口
public interface UserMapper {
// 1、 根據(jù)用戶(hù)ID查詢(xún)用戶(hù)信息
public User findUserById(int id) throws Exception;
// 2、 添加用戶(hù)
public void insertUser(User user) throws Exception;
}之后創(chuàng)建User的映射文件,在config下創(chuàng)建mapper目錄然后創(chuàng)建UserMapper.xml(這是mybatis的命名規(guī)范,當(dāng)然,也不是必須是這個(gè)名稱(chēng))。
由其規(guī)范可以確定其映射文件如下
(接口在包c(diǎn)om.itheima.mybatis.mapper.UserMapper下面):
<mapper namespace="com.itheima.mybatis.mapper.UserMapper">
<!-- 根據(jù)用戶(hù)ID查詢(xún)用戶(hù)信息 -->
<select id="findUserById" parameterType="int" resultType="User">
SELECT
* FROM USER WHERE id =#{id}
</select>
?
<!-- 添加用戶(hù) -->
<insert id="insertUser" parameterType="com.itheima.mybatis.po.User">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
?
INSERT INTO USER
(username,birthday,sex,address)
VALUES(#{username},#{birthday},#{sex},#{address})
</insert>
</mapper>之后將映射文件加到全局配置文件即可。
然后便可以進(jìn)行測(cè)試
public class UserMapperTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void setUp() throws Exception {
// 讀取配置文件
// 全局配置文件的路徑
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 創(chuàng)建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
?
@Test
public void testFindUserById() throws Exception {
// 創(chuàng)建UserMapper對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession();
?
// 由mybatis通過(guò)sqlsession來(lái)創(chuàng)建代理對(duì)象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.findUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void testInsertUser() throws Exception {
// 創(chuàng)建UserMapper對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 由mybatis通過(guò)sqlsession來(lái)創(chuàng)建代理對(duì)象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("東哥hm19");
user.setAddress("寶盛西里24號(hào)樓");
mapper.insertUser(user);
System.out.println(user.getId());
sqlSession.commit();
sqlSession.close();
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中FileWriter類(lèi)的常用方法說(shuō)明
這篇文章主要介紹了Java中FileWriter類(lèi)的常用方法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
MyBatis自定義TypeHandler如何解決字段映射問(wèn)題
這篇文章主要介紹了MyBatis自定義TypeHandler如何解決字段映射問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
解決SpringBoot啟動(dòng)警告:OpenJDK?64-Bit?Server?VM?warning
這篇文章主要介紹了解決SpringBoot啟動(dòng)警告:OpenJDK?64-Bit?Server?VM?warning的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)同樣遇到這個(gè)問(wèn)題的朋友具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-11-11
Java AQS中ReentrantReadWriteLock讀寫(xiě)鎖的使用
ReentrantReadWriteLock稱(chēng)為讀寫(xiě)鎖,它提供一個(gè)讀鎖,支持多個(gè)線(xiàn)程共享同一把鎖。這篇文章主要講解一下ReentrantReadWriteLock的使用和應(yīng)用場(chǎng)景,感興趣的可以了解一下2023-02-02
SpringBoot3配置Logback日志滾動(dòng)文件的方法
本文介紹了在SpringBoot3中配置Logback日志滾動(dòng)文件的方法,因?yàn)镾pringBoot3內(nèi)置的logback版本是1.4.14,之前使用SpringBoot2.1.5的logback配置發(fā)現(xiàn)有些東西不能生效了,需要的朋友可以參考下2024-08-08
SpringBoot使用自定義注解+AOP+Redis實(shí)現(xiàn)接口限流的實(shí)例代碼
這篇文章主要介紹了SpringBoot使用自定義注解+AOP+Redis實(shí)現(xiàn)接口限流,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09

