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映射器,然后調用SqlSession的getMapper()方法,將該映射器納入到spring管理,默認的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ù)據(jù)庫字段名不一致,完成字段名的對應--> <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.結果演示
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的詳細內容,更多關于Spring整合Mybatis的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. Java8內存模型PermGen Metaspace實例解析2. 利用單元測試對PHP代碼進行檢查3. python如何實現(xiàn)word批量轉HTML4. python excel和yaml文件的讀取封裝5. python3實現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)6. moment轉化時間戳出現(xiàn)Invalid Date的問題及解決7. python爬蟲實戰(zhàn)之制作屬于自己的一個IP代理模塊8. python操作數(shù)據(jù)庫獲取結果之fetchone和fetchall的區(qū)別說明9. Django 權限管理(permissions)與用戶組(group)詳解10. App啟動優(yōu)化-Android性能優(yōu)化
