[TOC]
框架技術(shù)
持久化
mybatis概念
概念:一個持久層框架
作用:ORM將sql語句映射成實(shí)體類
特點(diǎn):巧靈活臼疫、半自動化找田、使用與中小型項(xiàng)目的開發(fā)
mybatis 入門
1嫂丙、創(chuàng)建mybatis-config.xml文件
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
2际歼、創(chuàng)建映射文件
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="user">
<select id="selectUser" resultType="com.hemi.mybatis.bean.User">
select * from user where uid=1;
</select>
</mapper>
3逆趣、獲取xml配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
4、創(chuàng)建SqlSessionFactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
5储藐、獲取SqlSession
SqlSession sqlSession = factory.openSession();
6、調(diào)用SqlSession的selectOne(命名空間.id名稱);
Object object = sqlSession.selectOne("user.selectUser");
7嘶是、關(guān)閉SqlSession
sqlSession.close();
增刪改
增
<insert id="insertUser" parameterType="com.hemi.mybatis.bean.User">
<!-- 通過#{屬性名}來獲取對象的值 -->
insert into user (username,password) values(#{username},#{password});
</insert>
改
<update id="updateUser" parameterType="com.hemi.mybatis.bean.User">
update user set username=#{username},password=#{password} where uid=#{uid}
</update>
刪
<delete id="deleteUser" parameterType="int">
delete from user where uid=#{value}
</delete>
Mapper接口開發(fā)
一钙勃、定義一個接口
public interface TypeMapper {
Type selectType(int typeid);
}
二、定義一個mapper.xml映射文件
mapper文件的要求:
1聂喇、namespace的值就是對象接口的全類名辖源,并且類名和xml文件名保持一致
2、id的值就是抽象方法
3、resultType的值必須和抽象方法的返回值一致
4克饶、parameterType的值和抽象方法的參數(shù)類型一致
注意 mapper.xml文件的約束是mapper.dtd酝蜒,不是config.dtd
三、使用
將mybatis入門步驟中的步驟六改為如下代碼:
TypeMapper mapper=sqlSession.getMapper(TypeMapper.class);
Type type=mapper.selectType(1);
動態(tài)sql
if
SELECT * FROM good INNER JOIN type ON good.type = type.typeid where 1=1
<if test="gname !=null and gname !=''">
and gname like concat('%',#{gname},'%')
</if>
注意:
1矾湃、字符串的拼接建議使用concat來代替${}
2亡脑、判斷條件中獲取數(shù)據(jù)不用加#{},與el表達(dá)式不一樣
where
作用where可以自動去除第一個and
<where>
<if test="gname !=null and gname !=''">
and gname like concat('%',#{gname},'%')
</if>
<if test="typename !=null and typename!=''">
and typename like concat('%',#{typename},'%')
</if>
</where>
choose when otherwise
作用:組合使用洲尊,相當(dāng)于if else if else
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = 'ACTIVE'
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
set
update good
<set>
<if test="gname!=null and gname!=''">
gname=#{gname},
</if>
<if test="gprice!=null and gprice!=''">
gprice=#{gprice}
</if>
</set>
where gid=#{gid}
trim
作用:去除多余字符串
兩種常見的用法
1远豺、where and
prefix:字首 prefixOverrides:去除第一個指定的字符串
select * from good
<trim prefix="where" prefixOverrides="and|or">
<!--添加where在前面,并且去除第一個and-->
<if test="gname !=null and gname !=''">
and gname like concat('%',#{gname},'%')
</if>
<if test="typename !=null and typename!=''">
and typename like concat('%',#{typename},'%')
</if>
</trim>
2坞嘀、set
prefix:字首 suffixOverrides:去除最后指定的字符串
update good
<trim prefix="set" suffixOverrides=",">
<!--添加set在前面躯护,并且去除最后一個,-->
<if test="gname!=null and gname!=''">
gname=#{gname},
</if>
<if test="gprice!=null and gprice!=''">
gprice=#{gprice},
</if>
</trim>
foreach
作用:動態(tài)循環(huán)拼接sql部分內(nèi)容
1、open代表在集合前面添加的字符串
2丽涩、close代表在集合后面添加的字符串
3棺滞、separator代表集合分割使用的字符串
4、collection代表被循環(huán)的集合矢渊,值可以是list继准、map、array
5矮男、常見用法移必,in的語句
<select id="selectGoodByGid" parameterType="list" resultType="Good">
select gid,gname,gprice,count,type typeid from good
<where>
gid in
<foreach item="item" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</where>
</select>
1、如何配置多個數(shù)據(jù)庫
使用environment可以配置多個數(shù)據(jù)庫毡鉴,通過ID來表示
2崔泵、transactionManager的type有哪些
JDBC、MANAGED
3猪瞬、dataSource作用
配置數(shù)據(jù)源憎瘸,有三種類型:UNPOOLED、POOLED陈瘦、JNDI還可以配置四大參數(shù)(驅(qū)動幌甘。url/用戶名、密碼)
4痊项、properties有什么用锅风,如何用
加載配置文件(resource/url),
配置鍵值對(proprerty)
注意:如果resource和proprerty鞍泉,resource優(yōu)先級高
5遏弱、typeAliases有什么用,如何用
起別名
用類名(類名)
6塞弊、databaseIdProvider + databaseid
7漱逸、databaseIdProvider:給數(shù)據(jù)庫起別名
8泪姨、databaseid:在statement中使用
myBatista根據(jù)url可以自動使用當(dāng)前是什么數(shù)據(jù)庫,然后使用哪個sql語句
9饰抒、mapper中的namespace有什么用
如果有相同的statementid的時候肮砾,使用namespace類區(qū)別
使用mapper接口開發(fā)dao時,namespace與接口全類名一致
10袋坑、增刪改查
id:statement的唯一標(biāo)識仗处,或者說是調(diào)用方法名
parameterType:傳入?yún)?shù)的數(shù)據(jù)類型 基本數(shù)據(jù)(可以不寫)+引用類型(必須寫)
resultType:返回?cái)?shù)據(jù)類型
parameterType和resultType:都可以使用別名來表示數(shù)據(jù)類名
11、myBatista中的數(shù)據(jù)類型的別名
Integer Integer枣宫,int
int _int
12婆誓、#{}作用
獲取傳入數(shù)據(jù)
13、Mapper接口開發(fā)的四大要求
1也颤、namespace的值就是對象接口的全類名洋幻,并且類名和xml文件名保持一致
2、id的值就是抽象方法
3翅娶、resultType的值必須和抽象方法的返回值一致
4文留、parameterType的值和抽象方法的參數(shù)類型一致