---恢复内容开始---
# mybatis入门 - 1.应用程序找mybatis要数据 - 2.mybatis从数据库中找来数据 (2.1通过mybatis-config.xml定位哪个数据库 2.2通过映射文件(Category.xml)执行对应的select语句 2.3把多个Category对象装在一个category集合中) - 3.返回一个Category集合具体的步骤:
- 1.需要的jar包(mybatis-3.4.2.jar和 mysql-connector-java-5.08-bin.jar)
- 2.创建实体类(Category,用于映射category_表)
- 3.配置文件mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--别名,自动扫描com. kk.pojo下的类型,使得在后续的映射配置文件中可以直接使用category,而不需全写-->
<typeAliases>
<package name="com.kk.pojo"/>
</typeAliases>
<!--作用主要是提供连接数据库用的驱动,数据库名称,编码方式,账号密码-->
<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/how2java?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<!--映射Category.xml文件-->
<mappers>
<mapper resource="com/how2java/pojo/Category.xml"/>
</mappers>
</configuration>
- 4.配置文件Category.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kk.pojo"> <!--表示命名空间,表示哪个包下面的类映射到这个文件-->
<select id="listCategory" resultType="Category"> <!--后续的sql语句的id,用于标识此sql语句供后续代码调用,resultType="Category"表示返回的数据和Category关联起来-->
select * from category_
</select>
</mapper>
- 5.测试类TestMybatis
public class TestMybatis {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //根据配置文件mybatis-config.xml得到sqlSessionFactory
SqlSession session=sqlSessionFactory.openSession(); //根据sqlSessionFactory得到session
List<Category> cs=session.selectList("listCategory"); //listCategory这个就是在配置文件Category.xml中那条sql语句设置的id。
for (Category c : cs) {
System.out.println(c.getName());
}
}
}
CRUD(增删改查)
- 1.配置文件category.xml文件
<insert id="addCategory" parameterType="Category" > <!--增加一条数据-->
insert into category_ ( name ) values (#{name})
</insert>
<delete id="deleteCategory" parameterType="Category" > <!--删除一条数据-->
delete from category_ where id= #{id}
</delete>
<!--这是mybatis 内置别名_int 代表 java中的基本类型int,int 代表 java中的类类型Integer-->
<select id="getCategory" parameterType="_int" resultType="Category"> <!--得到一条特定的数据-->
select * from category_ where id= #{id}
</select>
<update id="updateCategory" parameterType="Category" > <!--更新某条数据-->
update category_ set name=#{name} where id=#{id}
</update>
<select id="listCategory" resultType="Category">
select * from category_
</select>
- 对应的测试代码:
//增加
Category c = new Category();
c.setName("新增加的Category");
session.insert("addCategory",c);
//删除,删除id为6的数据
Category c = new Category();
c.setId(6);
session.delete("deleteCategory",c);
//获取id=3的记录
Category c= session.selectOne("getCategory",3);
//修改
session.update("updateCategory",c);
//查询所有
List<Category> cs = session.selectList("listCategory");
动态SQL
if语句
若mapper中的xml文件中的sql语句不使用if时,需要分开查询,需要写多条语句。
使用if可以把不同情况的查询语句整合,例如可以把全部查询和模糊查询相结合。
<select id="listProduct" resultType="Product">
select * from product_
</select>
<select id="listProductByName" resultType="Product">
select * from product_ where name like concat('%',#{name},'%')
</select>
若使用if可以做到:
<select id="listProduct" resultType="Product">
select * from product_
<if test="name!=null">
where name like concat('%',#{name},'%')
</if>
</select>
where语句
where用于多条件查询时,若是使用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>
若,当name无参数时,就会报错,需要修改为下,使用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>
set标签
与where标签类似的,在update语句里也会碰到多个字段相关的问题。 在这种情况下,就可以使用set标签:
<update id="updateProduct" parameterType="Product" >
update product_
<set>
<if test="name != null">name=#{name},</if>
<if test="price != null">price=#{price}</if>
</set>
trim标签
trim 用来定制想要的功能,比如where标签就可以用如下替换
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
set标签,可以用如下替换
<trim prefix="SET" suffixOverrides=",">
...
</trim>
choose标签
Mybatis里面没有else标签,但是可以使用when otherwise标签来达到这样的效果。
<select id="listProduct" resultType="Product">
SELECT * FROM product_
<where>
<choose>
<when test="name != null">
and name like concat('%',#{name},'%')
</when>
<when test="price !=null and price != 0">
and price > #{price}
</when>
<otherwise>
and id >1
</otherwise>
</choose>
</where>
</select>
bind标签
bind标签就像是再做一次字符串拼接,方便后续使用
<select id="listpt" resultType="Product">
select * from product_ where name like concat('%', #{0} , '%')
</select>
<select id="listProduct" resultType="Product">
<bind name="likename" value="'%' + name + '%'" />
select * from product_ where name like #{likename}
</select>
```<p>---恢复内容结束---</p>