目录
1、maven新建一个工程编辑
2、添加POM.XML配置文件
3、创建实例包
4、创建一个环境资源根目录
5、配置环境文件
6、创建接口,添加方法
7、编写sql语句
8、创建测试类
8.1 、定义工厂模式
8.2 、定义会话
8.3、定义对象
8.5、获取Builder建造工厂
8.6、通过建造工厂建造SqlSessionFactory
8.7、 创建Session会话
8.8、 获取Session会话的执行者
8.9、提交和释放资源
9、通过mysbatis实现sql语句
9.1、insert:
9.2、update
9.3、 delete
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语句
以下图为表结构
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>
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>
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);
}
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);
}