欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring整合Mybatis的全過程

 更新時(shí)間:2021年06月28日 11:28:18   作者:521125LYC  
這篇文章主要介紹了Spring整合Mybatis的全過程,包括spring配置文件書寫映射器接口的實(shí)例代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下

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ù)庫字段名不一致,完成字段名的對應(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對象

	ApplicationContext ac;
	UserMapper dao;
	@Before
	@Test
	public void test01() throws SQLException{
		//加載xml配置文件
		ac=new ClassPathXmlApplicationContext("spring-dao.xml");
		//獲取spring管理的javaBean對象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的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring Profiles使用方法詳解

    Spring Profiles使用方法詳解

    在你剛接觸SpringBoot的時(shí)候有沒有對它提供的Profile有些許不適應(yīng),經(jīng)過摸索后才領(lǐng)悟到它的強(qiáng)大。今天我就對Profile進(jìn)行一點(diǎn)歸納總結(jié),留作互聯(lián)網(wǎng)記憶
    2022-12-12
  • Java購物系統(tǒng)設(shè)計(jì)與實(shí)現(xià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ì)解析

    這篇文章主要介紹了Java中CountDownLatch工具類詳細(xì)解析,創(chuàng)建CountDownLatch對象時(shí),會(huì)傳入一個(gè)count數(shù)值,該對象每次調(diào)用countDown()方法會(huì)使count?--?,就是count每次減1,需要的朋友可以參考下
    2023-11-11
  • JavaWeb中的Cookie和Session解讀

    JavaWeb中的Cookie和Session解讀

    這篇文章主要介紹了JavaWeb中的Cookie和Session解讀,Cookie是servlet發(fā)送到Web瀏覽器的少量信息,該信息由瀏覽器保存,然后發(fā)送回服務(wù)器,一般情況下,Cookie是以鍵值對進(jìn)行表示的,Cookie的值可以唯一地標(biāo)識(shí)客戶端,因此Cookie常用于會(huì)話管理,需要的朋友可以參考下
    2023-10-10
  • Spring中Bean的命名方式代碼詳解

    Spring中Bean的命名方式代碼詳解

    這篇文章主要介紹了Spring中Bean的命名方式代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Java Spring Controller 獲取請求參數(shù)的幾種方法詳解

    Java Spring Controller 獲取請求參數(shù)的幾種方法詳解

    這篇文章主要介紹了Java Spring Controller 獲取請求參數(shù)的幾種方法詳解的相關(guān)資料,這里提供了6種方法,需要的朋友可以參考下
    2016-12-12
  • 通過Java代碼來創(chuàng)建view的方法

    通過Java代碼來創(chuàng)建view的方法

    本文給大家分享通過java代碼創(chuàng)建view的方法,以TextView為例創(chuàng)建控件的方法,需要的的朋友參考下吧
    2017-08-08
  • Mybatis中resultMap的Colum和property屬性詳解

    Mybatis中resultMap的Colum和property屬性詳解

    這篇文章主要介紹了Mybatis中resultMap的Colum和property屬性,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • Java事件機(jī)制要素及實(shí)例詳解

    Java事件機(jī)制要素及實(shí)例詳解

    這篇文章主要介紹了Java事件機(jī)制要素及實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • java實(shí)現(xiàn)圖片分割指定大小

    java實(shí)現(xiàn)圖片分割指定大小

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片分割指定大小,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評(píng)論