-
MyBatis使用XML步骤总结
1)配置mybatis-config.xml 全局的配置文件 (1、数据源,2、外部的mapper)
2)创建SqlSessionFactory
3)通过SqlSessionFactory创建SqlSession对象
4)通过SqlSession操作数据库 CRUD
5)调用session.commit()提交事务
6)调用session.close()关闭会话 -
MyBatis使用注解步骤总结
1)配置mybatis-config.xml 全局的配置文件 (1、数据源,2、外部的mapper)
1.1使用Mapper类加上方法和注解
public interface CategoryMapper {
@Insert(" insert into category_ ( name ) values (#{name}) ")
public int add(Category category);
@Delete(" delete from category_ where id= #{id} ")
public void delete(int id);
@Select("select * from category_ where id= #{id} ")
public Category get(int id);
@Update("update category_ set name=#{name} where id=#{id} ")
public int update(Category category);
@Select(" select * from category_ ")
public List<Category> list();
}
1.2 mybatis-config.xml配置Mapper类
<mappers>
<mapper class="pojo.CategoryMapper"/>
</mappers>
2)创建SqlSessionFactory
3)通过SqlSessionFactory创建SqlSession对象
4)通过SqlSession操作数据库 CRUD
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
CategoryMapper mapper = session.getMapper(CategoryMapper.class);
List<Category> cs = mapper.list();
cs.forEach(c -> System.out.println(c));
session.commit();
session.close();
5)调用session.commit()提交事务
6)调用session.close()关闭会话
- join语句的多对一和一对多
一对多:
<resultMap type="Category" id="categoryBean">
<id column="cid" property="id" />
<result column="cname" property="name" />
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="products" ofType="Product">
<id column="pid" property="id" />
<result column="pname" property="name" />
<result column="price" property="price" />
</collection>
</resultMap>
<!-- 关联查询分类和产品表 -->
<select id="listCategory" resultMap="categoryBean">
select
c.*,
p.*,
c.id 'cid',
p.id 'pid',
c.name 'cname',
p.name 'pname'
from category_ c left join product_ p on c.id = p.cid
</select>
多对一:同上,只不过用的标签是<association>
<collection>和<association>还可以不断地嵌套,实现多对多。
- 动态SQL
<if>:
可以查询全部或者模糊查询
<select id="listProduct" resultType="Product">
select * from product_
<if test="name != null">
where name like concat('%', #{name}, '%')
</if>
</select>
<where>:
当只有name没有price的时候下面的SQL语句会出错,因此我们需要<where>标签。
<select id="listProduct" resultType="Product">
select * from product_
<if test="name!=null">
where name like concat('%',#{name},'%')
</if>
<if test="price!=0">
and price > #{price}
</if>
</select>
<where>标签会进行自动判断
如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。
<select id="listProduct" resultType="Product">
select * from product_
<where>
<if test="name!=null">
and name like concat('%',#{name},'%')
</if>
<if test="price!=null and price!=0">
and price > #{price}
</if>
</where>
</select>
<set>:
与类似的,在update语句里也会碰到多个字段相关的问题。 在这种情况下,就可以使用set标签:
<set>
<if test="name != null">name=#{name},</if>
<if test="price != null">price=#{price}</if>
</set>
-
缓存
一级缓存:打开同一个session,取同样id的数据,只执行一次SQL
二级缓存:打开同一个SessionFactory,取同样id的数据,只执行一次SQL -
逆向工程generator工具
前面的学习都是先有pojo、mapper、xml,再创建表。
逆向工程是根据表再创建pojo、mapper、xml。
网友评论