【Java】 MyBatis的使用

一、ORM(Object Relation Mapping)框架

  1. ORM框架所解決的問題:阻抗不匹配(數據庫查詢所得到的數據和Java類型不匹配)
  2. ORM框架需要依賴於數據源(DataSource),常用的數據庫連接池:C3P0、Druid、DBCP
  3. 常見的ORM框架MyBatis 、 Hibernate 、 Spring JDBC 、 JPA

二、MyBatis初步使用

  1. 配置數據源
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${mysql.driverClassName}"></property>
        <property name="url" value="${mysql.url}"></property>
        <property name="username" value="${mysql.username}"></property>
        <property name="password" value="${mysql.password}"></property>
        <property name="maxWait" value="${mysql.maxWait}"></property>
        <property name="maxActive" value="${mysql.maxActive}"></property>
        <property name="minIdle" value="${mysql.minIdle}"></property>
    </bean>
  1. 配置MyBatis的SqlSessionFactoryBean
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
  1. 配置Mapper掃描
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
        <property name="basePackage" value="com.dao"></property>
    </bean>

三、多表聯查

  1. 創建vo對象,並定義結果集
    <resultMap id="UserVoMap" type="com.lanou.entity.vo.UserVo">
        <id property="uid" column="uid" jdbcType="VARCHAR"></id>
        <result property="username" column="username" jdbcType="VARCHAR"></result>
        <result property="address" column="address" jdbcType="VARCHAR"></result>
        <result property="gender" column="phone" jdbcType="VARCHAR"></result>
        <result property="phone" column="phone" jdbcType="VARCHAR"></result>
    </resultMap>
  1. 完成相應業務邏輯實現
	select 
		a.id as id,
		a.username as username,
		b.address as address,
		b.gender as gender,
		b.phone as phone  
	from 
		tb_user as a,
		tb_userinfo as b
	where 
		a.username=#{username}
		and a.password=#{password}
		and a.id=b.uid

四、一次性多表插入

方案一:多條語句插入

  1. 更改數據庫連接: allowMultiQueries=true
  2. 完成業務邏輯實現
<insert id="insertData" parameterType="java.util.Map">
	insert into tb_user(id,username,password) values (replace(uuid(),'-',''), #{user.username}, #{user.password});
	insert into tb_userinfo(
		id,
		gender,
		address,
		uid
	) values(
		replace(uuid(),'-',''),
		#{userinfo.gender},
		#{userinfo.address},
		(select id from tb_user where username=#{user.username}, {user.password})
	)
</insert>

方案二:使用存儲過程

  1. 將statement設置成callable
  2. call 相應的存儲過程

方案三:多次插入

  1. 將業務進行分層
	<!-- UserMapper.xml -->

    <select id="findIdByUsername" parameterType="java.lang.String" resultType="java.lang.String">
        select id from tb_user where username=#{username}
    </select>

    <insert id="insertUser" parameterType="com.lanou.entity.User">
        insert into
            tb_user( id, username, password)
        values
            (replace(uuid(), '-', ''), #{username}, #{password})
    </insert>

	<!-- UserInfoMapper.xml -->

    <insert id="insertUserInfo" parameterType="com.entity.vo.UserVo">
        insert into
            tb_userinfo(uid, address, gender, phone)
        values
            (#{uid}, #{address}, #{gender}, #{phone})
    </insert>

2.在業務層執行相應邏輯

    @Override
    public void addUserInfo(User user, UserVo userVo) {

    	//第一步:先對user表進行插入
        userDao.insertUser(user);

        //第二步:查詢用戶生成的id,並將該id設置到即將插入userinfo表的對象中
        userVo.setUid(userDao.findIdByUsername(user.getUsername()));

        //第三步:執行userinfo表的插入操作
        userInfoDao.insertUserInfo(userVo);
    }
發佈了73 篇原創文章 · 獲贊 282 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章