Spring整合Mybatis的全過程
1.1配置數據庫連接池
<!--讀取文件--> <util:properties location='classpath:Config/db.properties'/> <!--配置數據庫連接池--> <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配置數據源工廠
<!--配置sqlsessionFactoryBean--> <bean class='org.mybatis.spring.SqlSessionFactoryBean'> <!--配置映射文件(操作sql語句的文件)的位置--> <property name='mapperLocations' value='classpath:mapper/user-mapper.xml'/> <!-- 將連接池注入到該數據源屬性中--> <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);//根據用戶id查詢用戶數據User selectUserById(int id);//查詢所有用戶數據List<User> findAllUser();//根據用戶名和密碼查詢用戶數據,返回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'> <!--實體類和數據庫字段名不一致,完成字段名的對應--> <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> <!-- 根據用戶id查詢用戶數據 --> <select resultMap='rm'> select * from u_user where id=#{id}; </select> <!-- 查詢所有用戶數據 --> <select resultMap='rm'> select * from u_user; </select> <!-- 根據用戶名和密碼查詢用戶數據,返回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.根據用戶id查詢用戶數據
@Testpublic void test03(){User u=dao.selectUserById(1);System.out.println(u);}
查找成功
4.查詢所有用戶數據
@Testpublic void test04(){List<User> list=dao.findAllUser();System.out.println(list);}
查詢到所有的用戶數據
5.根據用戶名和密碼查詢用戶數據,返回map集合@Testpublic void test05(){Map<String,Object> map=dao.findUserByNameAndPwd('何倩','125521');System.out.println(map);}
查詢成功
以上就是Spring整合Mybatis的詳細內容,更多關于Spring整合Mybatis的資料請關注好吧啦網其它相關文章!
相關文章:
