Spring整合Mybatis的全過程
1.Spring配置文件
1.1配置數(shù)據(jù)庫連接池
<!--讀取文件-->
<util:properties id="config" location="classpath:Config/db.properties"/>
<!--配置數(shù)據(jù)庫連接池-->
<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語句的文件)的位置--> <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是映射器首字母小寫的接口名。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="fyjz.com.springMybatis.mapper"/> </bean>
2.書寫映射器(接口)
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 {
//用戶登錄
int addUser(User user);
//根據(jù)用戶id查詢用戶數(shù)據(jù)
User selectUserById(int id);
//查詢所有用戶數(shù)據(jù)
List<User> findAllUser();
//根據(jù)用戶名和密碼查詢用戶數(shù)據(jù),返回map集合
Map<String,Object> findUserByNameAndPwd(@Param("name")String name,@Param("pwd")String pwd);
}
3.書寫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">
<!--映射文件(映射器的全名:包名.類名)-->
<mapper namespace="fyjz.com.springMybatis.mapper.UserMapper">
<!--實(shí)體類和數(shù)據(jù)庫字段名不一致,完成字段名的對(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>
<!-- 添加用戶信息 -->
<insert id="addUser" parameterType="fyjz.com.springMybatis.entry.User">
insert into u_user values(null,#{userName},#{userPwd},#{money},#{age});
</insert>
<!-- 根據(jù)用戶id查詢用戶數(shù)據(jù) -->
<select id="selectUserById" resultMap="rm">
select * from u_user where id=#{id};
</select>
<!-- 查詢所有用戶數(shù)據(jù) -->
<select id="findAllUser" resultMap="rm">
select * from u_user;
</select>
<!-- 根據(jù)用戶名和密碼查詢用戶數(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.添加用戶信息
@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ù)用戶id查詢用戶數(shù)據(jù)
@Test
public void test03(){
User u=dao.selectUserById(1);
System.out.println(u);
}

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

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

查詢成功
以上就是Spring整合Mybatis的詳細(xì)內(nèi)容,更多關(guān)于Spring整合Mybatis的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 解決Spring boot整合mybatis,xml資源文件放置及路徑配置問題
- SpringBoot整合mybatis-generator-maven-plugin的方法
- springboot整合mybatis-plus 實(shí)現(xiàn)分頁查詢功能
- Spring Boot整合mybatis使用注解實(shí)現(xiàn)動(dòng)態(tài)Sql、參數(shù)傳遞等常用操作(實(shí)現(xiàn)方法)
- springboot2.3 整合mybatis-plus 高級(jí)功能(圖文詳解)
- 解決SpringBoot整合Mybatis掃描不到Mapper的問題
- SpringBoot如何通過yml方式整合Mybatis
- SpringBoot整合MybatisSQL過濾@Intercepts的實(shí)現(xiàn)
相關(guān)文章
Java購物系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java購物系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Java中CountDownLatch工具類詳細(xì)解析
這篇文章主要介紹了Java中CountDownLatch工具類詳細(xì)解析,創(chuàng)建CountDownLatch對(duì)象時(shí),會(huì)傳入一個(gè)count數(shù)值,該對(duì)象每次調(diào)用countDown()方法會(huì)使count?--?,就是count每次減1,需要的朋友可以參考下2023-11-11
Java Spring Controller 獲取請(qǐng)求參數(shù)的幾種方法詳解
這篇文章主要介紹了Java Spring Controller 獲取請(qǐng)求參數(shù)的幾種方法詳解的相關(guān)資料,這里提供了6種方法,需要的朋友可以參考下2016-12-12
Mybatis中resultMap的Colum和property屬性詳解
這篇文章主要介紹了Mybatis中resultMap的Colum和property屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01

