Spring整合Mybatis的全過(guò)程
1.Spring配置文件
1.1配置數(shù)據(jù)庫(kù)連接池
<!--讀取文件-->
<util:properties id="config" location="classpath:Config/db.properties"/>
<!--配置數(shù)據(jù)庫(kù)連接池-->
<bean id="source" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{config.drivername}"/>
<property name="url" value="#{config.url}"/>
<property name="username" value="#{config.name}"/>
<property name="password" value="#{config.password}"/>
<property name="maxActive" value="#{config.maxActive}"/>
<property name="maxWait" value="#{config.maxWait}"/>
</bean>
1.2配置數(shù)據(jù)源工廠
<!--配置sqlsessionFactoryBean--> <bean id="sqlsession" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--配置映射文件(操作sql語(yǔ)句的文件)的位置--> <property name="mapperLocations" value="classpath:mapper/user-mapper.xml"/> <!-- 將連接池注入到該數(shù)據(jù)源屬性中--> <property name="dataSource" ref="source"/> </bean>
1.3配置MapperScannerConfigurer
配置MapperScannerConfigurer,掃描指定包及其子包下面的所有Mapper映射器,然后調(diào)用SqlSession的getMapper()方法,將該映射器納入到spring管理,默認(rèn)的id是映射器首字母小寫(xiě)的接口名。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="fyjz.com.springMybatis.mapper"/> </bean>
2.書(shū)寫(xiě)映射器(接口)
package fyjz.com.springMybatis.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import fyjz.com.springMybatis.entry.User;
public interface UserMapper {
//用戶(hù)登錄
int addUser(User user);
//根據(jù)用戶(hù)id查詢(xún)用戶(hù)數(shù)據(jù)
User selectUserById(int id);
//查詢(xún)所有用戶(hù)數(shù)據(jù)
List<User> findAllUser();
//根據(jù)用戶(hù)名和密碼查詢(xún)用戶(hù)數(shù)據(jù),返回map集合
Map<String,Object> findUserByNameAndPwd(@Param("name")String name,@Param("pwd")String pwd);
}
3.書(shū)寫(xiě)user-mapper.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!--映射文件(映射器的全名:包名.類(lèi)名)-->
<mapper namespace="fyjz.com.springMybatis.mapper.UserMapper">
<!--實(shí)體類(lèi)和數(shù)據(jù)庫(kù)字段名不一致,完成字段名的對(duì)應(yīng)-->
<resultMap type="fyjz.com.springMybatis.entry.User" id="rm">
<result property="id" column="id"/>
<result property="userName" column="user_name"/>
<result property="userPwd" column="user_pwd"/>
<result property="money" column="money"/>
<result property="age" column="age"/>
</resultMap>
<!-- 添加用戶(hù)信息 -->
<insert id="addUser" parameterType="fyjz.com.springMybatis.entry.User">
insert into u_user values(null,#{userName},#{userPwd},#{money},#{age});
</insert>
<!-- 根據(jù)用戶(hù)id查詢(xún)用戶(hù)數(shù)據(jù) -->
<select id="selectUserById" resultMap="rm">
select * from u_user where id=#{id};
</select>
<!-- 查詢(xún)所有用戶(hù)數(shù)據(jù) -->
<select id="findAllUser" resultMap="rm">
select * from u_user;
</select>
<!-- 根據(jù)用戶(hù)名和密碼查詢(xún)用戶(hù)數(shù)據(jù),返回map集合-->
<select id="findUserByNameAndPwd" resultType="map">
select * from u_user where user_name=#{name} and user_pwd=#{pwd};
</select>
</mapper>
4.結(jié)果演示
1.加載Spring配置文件并生成javaBean對(duì)象
ApplicationContext ac;
UserMapper dao;
@Before
@Test
public void test01() throws SQLException{
//加載xml配置文件
ac=new ClassPathXmlApplicationContext("spring-dao.xml");
//獲取spring管理的javaBean對(duì)象userMapper
dao=ac.getBean("userMapper",UserMapper.class);
}
2.添加用戶(hù)信息
@Test
public void test02(){
User u=new User(0, "uzi","52147893", 52360, 50);
int n=dao.addUser(u);
System.out.println(n);
}

插入成功,后臺(tái)返回1
3.根據(jù)用戶(hù)id查詢(xún)用戶(hù)數(shù)據(jù)
@Test
public void test03(){
User u=dao.selectUserById(1);
System.out.println(u);
}

查找成功
4.查詢(xún)所有用戶(hù)數(shù)據(jù)
@Test
public void test04(){
List<User> list=dao.findAllUser();
System.out.println(list);
}

查詢(xún)到所有的用戶(hù)數(shù)據(jù)
5.根據(jù)用戶(hù)名和密碼查詢(xún)用戶(hù)數(shù)據(jù),返回map集合
@Test
public void test05(){
Map<String,Object> map=dao.findUserByNameAndPwd("何倩","125521");
System.out.println(map);
}

查詢(xún)成功
以上就是Spring整合Mybatis的詳細(xì)內(nèi)容,更多關(guān)于Spring整合Mybatis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 解決Spring boot整合mybatis,xml資源文件放置及路徑配置問(wèn)題
- SpringBoot整合mybatis-generator-maven-plugin的方法
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁(yè)查詢(xún)功能
- Spring Boot整合mybatis使用注解實(shí)現(xiàn)動(dòng)態(tài)Sql、參數(shù)傳遞等常用操作(實(shí)現(xiàn)方法)
- springboot2.3 整合mybatis-plus 高級(jí)功能(圖文詳解)
- 解決SpringBoot整合Mybatis掃描不到Mapper的問(wèn)題
- SpringBoot如何通過(guò)yml方式整合Mybatis
- SpringBoot整合MybatisSQL過(guò)濾@Intercepts的實(shí)現(xiàn)
相關(guān)文章
Jmeter接口登錄獲取參數(shù)token報(bào)錯(cuò)問(wèn)題解決方案
這篇文章主要介紹了Jmeter接口登錄獲取參數(shù)token報(bào)錯(cuò)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
SpringSecurity自定義登錄接口的實(shí)現(xiàn)
本文介紹了使用Spring Security實(shí)現(xiàn)自定義登錄接口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
詳解Spring Boot下使用logback 記錄多個(gè)文件日志
這篇文章主要介紹了詳解Spring Boot下使用logback 記錄多個(gè)文件日志,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
java簡(jiǎn)單實(shí)現(xiàn)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)單實(shí)現(xiàn)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Spring?Security權(quán)限管理實(shí)現(xiàn)接口動(dòng)態(tài)權(quán)限控制
這篇文章主要為大家介紹了Spring?Security權(quán)限管理實(shí)現(xiàn)接口動(dòng)態(tài)權(quán)限控制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

