目录
1、maven新建一个工程
2、添加POM.XML配置文件
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
3、创建实例包
4、创建一个环境资源根目录
补充好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>
<properties resource="database.properties"></properties>
<typeAliases>
<package name="nj.zb.kb21.pojo"/>
</typeAliases>
<environments default="mysqlDev">
<environment id="mysqlDev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mysqldriver}"/>
<property name="url" value="${mysqlurl}"/>
<property name="username" value="${mysqluser}"/>
<property name="password" value="${mysqlpwd}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="nj/zb/kb21/dao/DogDao.xml"/>
<mapper resource="nj/zb/kb21/dao/MasterDao.xml"/>
</mappers>
</configuration>
创建一个xml文件,Mapper里面要添加进去,上面代码已经创了以下两个
<mapper resource="nj/zb/kb21/dao/DogDao.xml"/>
<mapper resource="nj/zb/kb21/dao/MasterDao.xml"/>
5、配置环境文件
只需添加以下内容,insert和update为后面接口实现sql语句
<?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="nj.zb.kb21.dao.DogDao">
</mapper>
6、创建接口,添加方法
7、编写sql语句
以下图为表结构
以下为dog和master属性
DogDao接口的所有语句
insert 添加语句
- 添加一个
<insert id="save">
insert into dog(name, health, love, strain, lytm)
values (#{name}, #{health}, #{love}, #{strain}, now())
</insert>
- 批量添加
<sql id="tableName">dog</sql>
<insert id="batchAdd">
insert into
<include refid="tableName"/>
-- insert into dog(name,health,love,strain,lytm)
values
<foreach collection="list" item="dog" separator=",">
(null,#{dog.name},#{dog.health},#{dog.love},#{dog.strain},now())
</foreach>
</insert>
- 动态添加
<insert id="add">
insert into dog
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name!=null and name!='' ">
name,
</if>
<if test="health!=null">
health,
</if>
<if test="love!=null">
love,
</if>
<if test="strain!=null and strain!='' ">
strain,
</if>
<if test="lytm!=null">
lytm,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name!=null and name!='' ">
#{name},
</if>
<if test="health!=null">
#{health},
</if>
<if test="love!=null">
#{love},
</if>
<if test="strain!=null and strain!='' ">
#{strain},
</if>
<if test="lytm!=null">
#{lytm},
</if>
</trim>
</insert>
update修改语句
- 修改一个
<update id="update">
update dog
set name=#{name},
health=#{health}
where id = #{id}
</update>
- 批量修改/动态修改
<update id="batchUpdate">
<foreach collection="list" item="dog" separator=";">
update
<include refid="tableName"/>
<set>
<if test="dog.name!=null and dog.name!='' ">
name=#{dog.name},
</if>
<if test="dog.health!=null">
health=#{dog.health},
</if>
<if test="dog.love!=null">
love=#{dog.love},
</if>
<if test="dog.strain!=null and dog.strain!='' ">
strain=#{dog.strain},
</if>
</set>
where id=#{dog.id}
</foreach>
</update>
delete删除语句
- 删除一条
<delete id="Delete">
delete
from dog
where id = #{id}
</delete>
- 批量删除
<delete id="batchDelete">
delete from dog where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
select查询语句
- 查询所有的狗狗
<select id="findAllDog" resultType="nj.zb.kb21.pojo.Dog">
select id, name, health, love, strain, lytm
from dog;
</select>
-
根据狗狗id找出狗狗的详细信息以及所有领养过此狗狗的所有主人信息(集合)
<resultMap id="dogMap" type="Dog">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="health" property="health"></result>
<result column="love" property="love"></result>
<result column="strain" property="strain"></result>
<result column="lytm" property="lytm"></result>
<collection property="masters" ofType="Master">
<id column="pid" property="pid"></id>
<result column="uname" property="name"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<result column="yearnum" property="yearnum"></result>
<result column="did" property="did"></result>
</collection>
</resultMap>
<select id="getDogAndMastersByDogId" resultMap="dogMap">
select d.id,
d.name,
d.health,
d.love,
d.strain,
d.lytm,
m.pid,
m.name uname,
m.age,
m.gender,
m.yearnum,
m.did
from dog d
left join master m on m.did = d.id
where id = #{dogId}
</select>
- 根据狗狗的品种找出狗狗的详细信息以及所有领养过此狗狗的所有主人信息(集合)
测试类与查id不同,需要循环嵌套循环
<select id="getDogAndMastersByDogStrain" resultMap="dogMap">
select d.id,
d.name,
d.health,
d.love,
d.strain,
d.lytm,
m.pid,
m.name uname,
m.age,
m.gender,
m.yearnum,
m.did
from dog d
left join master m on m.did = d.id
where strain = #{dogStrain}
</select>
- 根据狗狗的名字(模糊查询)和亲密度 查询狗狗信息 可以自由选择降序和升序(1,升序 2,降序)
<select id="getDogByNameAndLove" resultMap="dogMap">
select id, name, health, love, strain, lytm
from dog
<where>
<if test="dogname!=null and dogname!='' ">
name like concat('%',#{dogname},'%')
</if>
<if test="doglove!=null">
and love=#{doglove}
</if>
</where>
<choose>
<when test="ordervalue=1">
order by lytm asc
</when>
<when test="ordervalue=2">
order by lytm desc
</when>
<otherwise>
order by id desc
</otherwise>
</choose>
</select>
- dog List<Master> StrainInfo 三表联查,此处增加一个狗的种类表
<resultMap id="dogMap2" type="Dog">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="health" property="health"></result>
<result column="love" property="love"></result>
<result column="strain" property="strain"></result>
<result column="lytm" property="lytm"></result>
<association property="strainType" javaType="StrainType">
<id column="sid" property="id"></id>
<result column="sname" property="name"></result>
<result column="describe" property="describe"></result>
</association>
<collection property="masters" ofType="Master">
<id column="pid" property="pid"></id>
<result column="uname" property="name"></result>
<result column="age" property="age"></result>
<result column="gender" property="gender"></result>
<result column="yearnum" property="yearnum"></result>
<result column="did" property="did"></result>
</collection>
</resultMap>
<select id="getDogAndMastersAndStrainByDogId" resultMap="dogMap2">
select d.id,
d.name,
d.health,
d.love,
d.strain,
d.lytm,
s.id sid,
s.name sname,
s.describe,
m.pid,
m.name uname,
m.age,
m.gender,
m.yearnum,
m.did
from dog d
left join straininfo s on d.strain = s.name
left join master m on d.id = m.did
where d.id = #{dogId};
</select>
8、创建测试类
8.1 、定义工厂模式
SqlSessionFactory sqlSessionFactory=null;
8.2 、定义会话
SqlSession sqlSession=null;
8.3、定义对象
DogDao dogDao=null;
8.4、加载mybatis-config.xml 配置文件
InputStream inputStream = DogTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
8.5、获取Builder建造工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
8.6、通过建造工厂建造SqlSessionFactory
sqlSessionFactory = builder.build(inputStream);
8.7、 创建Session会话
sqlSession = sqlSessionFactory.openSession();
8.8、 获取Session会话的执行者
dogDao = sqlSession.getMapper(DogDao.class);
8.9、提交和释放资源
完整代码:
public class DogTest {
//工厂模式
SqlSessionFactory sqlSessionFactory=null;
//定义会话
SqlSession sqlSession=null;
//定义DogDao对象
DogDao dogDao=null;
@Before
public void init(){
System.out.println("Start");
//加载mybatis-config.xml 配置文件
InputStream inputStream = DogTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
//获取Builder建造工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//通过建造工厂建造SqlSessionFactory
sqlSessionFactory = builder.build(inputStream);
//创建Session会话
sqlSession = sqlSessionFactory.openSession();
//获取Session会话的执行者
dogDao = sqlSession.getMapper(DogDao.class);
}
}
@After
public void down(){
sqlSession.commit();
sqlSession.close();
System.out.println("Over");
}
9、通过mysbatis实现sql语句
9.1、insert:
- 新增一个:
@Test
public void testSaveDog(){
Dog dog = new Dog();
dog.setName("冰糖雪梨");
dog.setHealth(100);
dog.setLove(100);
dog.setStrain("冰糖雪梨");
dogDao.save(dog);
}
- 批量新增:
@Test
public void testBatchAddDog(){
List<Dog> dogs=new ArrayList<Dog>();
Dog dog = new Dog("农夫山泉", 100, 100, "有点甜");
Dog dog2 = new Dog("娃哈哈", 100, 100, "爱的供养");
Dog dog3 = new Dog("百岁山", 98, 88, "水中贵族");
Dog dog4 = new Dog("怡宝", 99, 99, "蒸馏水");
dogs.add(dog);
dogs.add(dog2);
dogs.add(dog3);
dogs.add(dog4);
dogDao.batchAdd(dogs);
}
- 动态新增
@Test
public void testAddeDog(){
Dog dog = new Dog();
dog.setId(4);
dog.setName("仔仔");
dog.setStrain("仔仔");
dog.setLytm(new Date(System.currentTimeMillis()));
dogDao.save(dog);
}
9.2、update
- 修改一个
@Test
public void testUpdateDog(){
Dog dog = new Dog();
dog.setId(19);
dog.setName("冰糖葫芦");
dog.setHealth(999);
dogDao.update(dog);
}
- 批量修改/动态修改
@Test
public void testBatchUpdateDog(){
List<Dog> dogs=new ArrayList<Dog>();
Dog dog = new Dog(22,"农夫山泉", 100, 100, "有点甜a");
Dog dog2 = new Dog(23,"娃哈哈", 10, 10, "爱的供养b");
Dog dog3 = new Dog(24,"百岁山", "水中贵族c");
Dog dog4 = new Dog(25,"怡宝", 9, 9, "蒸馏水d");
dogs.add(dog);
dogs.add(dog2);
dogs.add(dog3);
dogs.add(dog4);
dogDao.batchUpdate(dogs);
}
9.3、 delete
- 删除一个
@Test
public void testDeleteDog(){
Integer id=16;
dogDao.Delete(id);
}
- 批量删除
@Test
public void testBatchDeleteDog(){
List<Integer> dogs=new ArrayList<>();
dogs.add(17);
dogs.add(18);
dogs.add(20);
dogDao.batchDelete(dogs);
}
9.4、select
- 查询所有的狗狗
@Test
public void testFindAllDog(){
List<Dog> allDog = dogDao.findAllDog();
for (Dog dog:allDog
) {
System.out.println(dog.toString());
}
}
- 根据狗狗的品种找出狗狗的详细信息以及所有领养过此狗狗的所有主人信息(集合)
@Test
public void testDogAndMastersByDogId(){
Dog dog = dogDao.getDogAndMastersByDogId(7);
System.out.println("打印狗狗信息");
System.out.println(dog.toString());
System.out.println("打印主人信息");
List<Master> masters=dog.getMasters();
for (Master msater:masters
) {
System.out.println(msater.toString());
}
}
-
根据狗狗的品种找出狗狗的详细信息以及所有领养过此狗狗的所有主人信息(集合)
@Test
public void testDogAndMastersByDogStrain() {
List<Dog> dogs = dogDao.getDogAndMastersByDogStrain("德牧");
System.out.println("打印狗狗信息");
for (Dog dog : dogs
) {
System.out.println(dog.toString());
System.out.println("打印主人信息");
List<Master> masters = dog.getMasters();
for (Master msater : masters
) {
System.out.println(msater.toString());
}
}
}
-
根据狗狗的名字(模糊查询)和亲密度 查询狗狗信息 可以自由选择降序和升序(1,升序 2,降序)
@Test
public void testDogByNameAndLove(){
// List<Dog> dogs = dogDao.getDogByNameAndLove(null,null,null);
List<Dog> dogs = dogDao.getDogByNameAndLove("山",100,2);
for (Dog dog : dogs) {
System.out.println(dog.toString());
}
}
- dog List<Master> StrainInfo 三表联查,此处增加一个狗的种类表
@Test
public void testgetDogAndMastersAndStrainByDogId(){
Dog dog = dogDao.getDogAndMastersAndStrainByDogId(2);
System.out.println("狗狗信息");
System.out.println(dog.toString());
System.out.println("主人信息");
List<Master> masters = dog.getMasters();
for (Master master : masters) {
System.out.println(master.toString());
}
System.out.println("狗狗品种信息");
System.out.println(dog.getStrainType().toString());
}