1.使用environment可以配置多个数据库,用id来标识
2.transactionManager的type有哪些
JDBC,MANAGED
4.properties
1.加载配置文件
2.配置键值对
注意:如果resource和property,resource优先级高
5.typeAliases
1.起别名
用类名
6.databaseIdProvider+databaseid
1).给数据库起别名
2)在ststement中用别名
mydatis根据url可以自动当前使用的是什么数据库,然后使用哪个
7.mapper中使用namespace有什么用
1如果有相同的statementid的时候,使用namespace来区别
2.使用mapper使用接口开发dao时,namespace与接口全类名一致
8.增删改查
id:statement的唯一标识,或者说是我们调用的方法名
parameterType:传入参数的数据类型,基本数据类型+引用数据类型(必须写)
resultType:返回的数据类型
parameterType和resultTYpe都可以使用别名来表示数据类名
9.mabatis中数据类型的别名
Interger Interger,int
int _int
10.#{}的作用
获取传入数据
11.mapper接口开发的四大要求?
1.类名与namespace的值就是对象接口的全类名
2.id的值就是抽象方法
mybatis概念
mybatis入门
增删改
增
改
删
Mapper接口开发
动态sql
if
where
choose when otherwise
set
trim
foreach
mybatis概念
概念:一个持久层框架
作用:ORM将sql语句映射成实体类
特点:
巧灵活
半自动化
使用与中小型项目的开发
mybatis入门
1、创建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、创建映射文件UserMapper.xml
<!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、创建SqlSessionFactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
5、获取SqlSession
SqlSession sqlSession = factory.openSession();
6、调用SqlSession的selectOne(命名空间.id名称);
Object object = sqlSession.selectOne("user.selectUser");
7、关闭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>
删
Some_Text
<delete id="deleteUser" parameterType="int">
delete from user where uid=#{value}
</delete>
Mapper接口开发
一、定义一个接口
public interface TypeMapper {
Type selectType(int typeid);
}
二、定义一个mapper.xml映射文件
mapper文件的要求:
namespace的值就是对象接口的全类名,并且类名和xml文件名保持一致
id的值就是抽象方法
resultType的值必须和抽象方法的返回值一致
parameterType的值和抽象方法的参数类型一致
注意 mapper.xml文件的约束是mapper.dtd,不是config.dtd
三、使用
将mybatis入门步骤中的步骤六改为如下代码:
TypeMapper mapper=sqlSession.getMapper(TypeMapper.class);
Type type=mapper.selectType(1);
动态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、判断条件中获取数据不用加#{},与el表达式不一样
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
作用:组合使用,相当于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
作用:动态循环拼接sql部分内容
1、open代表在集合前面添加的字符串
2、close代表在集合后面添加的字符串
3、separator代表集合分割使用的字符串
4、collection代表被循环的集合,值可以是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>
resultMap
作用: 1、可以将表字段与javabean对象属性名进行映射 2、将映射级抽取出来,可以给多个statement所使用
1、字段名和属性名一致时可以不用写映射关系
mybatis有个配置,配置了自动映射
关联嵌套查询
<mapper namespace="com.dao.GoodMapper">
<select id="selectType" parameterType="String" resultMap="GoodResultMap">
select
gid,good_name,gprice,count,type from good where gid=1
</select>
<!-- 关联嵌套查询,把连接表分成两个表,这样效率更高 -->
<resultMap type="Good" id="GoodResultMap">
<result column="good_name" property="gname" />
<association property="type" javaType="Type" select="selectTypeById"
column="type"></association>
</resultMap>
<select id="selectTypeById" resultType="Type">
select * from type where
typeid=#{value}
</select>
</mapper>
多层嵌套
<mapper namespace="com.dao.UserOrderMapper">
<!--这里粘贴复制地址名后,记得注意验证 -->
<!-- 此处的id与同名Java类的接口中定义的要一致 -->
<select id="selectUserOrder" parameterType="list"
resultMap="UserOrderResultMapper">
<!-- 这里首先是建立一个新类UserOrder,连表查询 -->
select gorder.* ,user.user_name from gorder inner join user
on
gorder.uid=user.uid where gorder.uid=#{value}
<!-- 需要注意两个表中的重名问题 -->
</select>
<!-- 多层嵌套 -->
<resultMap type="UserOrder" id="UserOrderResultMapper">
<id column="uid" property="uid" />
<collection property="orderList" ofType="Order" resultMap="OrderResultMap">
<!--ofType这个属性用来区分 JavaBean(或字段)属性类型和集合包含的类型 -->
</collection>
<!-- resultMap 与id是对应的 -->
</resultMap>
<resultMap type="Order" id="OrderResultMap">
<id column="id" property="id" />
<result column="gid" property="gid" />
<result column="uid" property="uid" />
<result column="status" property="status" />
<result column="time" property="time" />
</resultMap>
</mapper>








网友评论