Spring整合Mybatis的全過程
1.1配置數(shù)據(jù)庫連接池
<!--讀取文件--> <util:properties location='classpath:Config/db.properties'/> <!--配置數(shù)據(jù)庫連接池--> <bean 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 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ù)庫字段名不一致,完成字段名的對應(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 parameterType='fyjz.com.springMybatis.entry.User'> insert into u_user values(null,#{userName},#{userPwd},#{money},#{age}); </insert> <!-- 根據(jù)用戶id查詢用戶數(shù)據(jù) --> <select resultMap='rm'> select * from u_user where id=#{id}; </select> <!-- 查詢所有用戶數(shù)據(jù) --> <select resultMap='rm'> select * from u_user; </select> <!-- 根據(jù)用戶名和密碼查詢用戶數(shù)據(jù),返回map集合--> <select resultType='map'> select * from u_user where user_name=#{name} and user_pwd=#{pwd}; </select> </mapper>4.結(jié)果演示
1.加載Spring配置文件并生成javaBean對象
ApplicationContext ac;UserMapper dao;@Before@Testpublic void test01() throws SQLException{//加載xml配置文件ac=new ClassPathXmlApplicationContext('spring-dao.xml');//獲取spring管理的javaBean對象userMapperdao=ac.getBean('userMapper',UserMapper.class);}
2.添加用戶信息
@Testpublic void test02(){User u=new User(0, 'uzi','52147893', 52360, 50);int n=dao.addUser(u);System.out.println(n);}

插入成功,后臺返回1
3.根據(jù)用戶id查詢用戶數(shù)據(jù)
@Testpublic void test03(){User u=dao.selectUserById(1);System.out.println(u);}

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

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

查詢成功
以上就是Spring整合Mybatis的詳細(xì)內(nèi)容,更多關(guān)于Spring整合Mybatis的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 基于android studio的layout的xml文件的創(chuàng)建方式2. 解決Android studio xml界面無法預(yù)覽問題3. 詳解Android studio 動態(tài)fragment的用法4. 圖文詳解vue中proto文件的函數(shù)調(diào)用5. 什么是python的自省6. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果(實(shí)例代碼)7. Android如何加載Base64編碼格式圖片8. 使用Android studio查看Kotlin的字節(jié)碼教程9. Vuex localStorage的具體使用10. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備