使用MyBatis做一个简单的增删改查
一、创建模板
1、操作概述:
点击File–>settings–>在上方搜索Template,点击下方的file and code Template–>点击“+”右边的Name写需要构建的模板名,Extension后面写文件的后缀名。下方文本域中写要构建的模板代码。
2、三大模板:
2.1、SqlMapConfig.xml
<configuration>
<properties resource="properties/db.properties">properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<typeAliases>
<typeAlias type="java.util.List" alias="list"/>
<package name="com.pojo"/>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="mapper/PetMapper.xml"/>
mappers>
configuration>
2.2、db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/k9503?zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123456
2.3、Mapper.xml
<mapper namespace="com.mapper.IPetDao">
<select id="selectPets" resultType="pet">
select * from pet
select>
<update id="update" parameterType="pet">
update pet set pname=#{pname},pbreed=#{pbreed},psex=#{psex},pbirthday=#{pbirthday} where pid=#{pid}
update>
<insert id="insert" parameterType="pet">
insert into pet(pname,pbreed,psex,pbirthday) values (pname=#{pname},pbreed=#{pbreed},psex=#{psex},pbirthday=#{pbirthday})
insert>
<delete id="delete" parameterType="_int">
delete from pet where pid=#{pid}
delete>
mapper>
二、使用Maven项目实现增删查改
该项目建包规范:
main包下:java包、resources包,test包、webApp包
java包下:com包
com包下:1、mapper包:用于存放Dao接口 2、pojo包:用于存放实体类文件
resources包下:1、mapper包:用于存放Mapper.xml映射文件 2、properties包:用于存放db.properties文件 3、sqlMapConfig.xml文件
test包下:再创建一个包名,包内存放测试类文件
1、Pet实体类
根据数据库字段名构建Pet的实体类,并提供get、set方法实现属性封装,另外,重写ToString方法
package com.pojo;
import java.util.Date;
public class Pet {
private int pid;
private String pname;
private int pbreed;
private int psex;
private Date pbirthday;
@Override
public String toString() {
return "Pet{" +
"pid=" + pid +
", pname='" + pname + '\'' +
", pbreed=" + pbreed +
", psex=" + psex +
", pbirthday=" + pbirthday +
'}';
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPbreed() {
return pbreed;
}
public void setPbreed(int pbreed) {
this.pbreed = pbreed;
}
public int getPsex() {
return psex;
}
public void setPsex(int psex) {
this.psex = psex;
}
public Date getPbirthday() {
return pbirthday;
}
public void setPbirthday(Date pbirthday) {
this.pbirthday = pbirthday;
}
}
2、IPetDao接口
在此构建增删查改的抽象方法,在测试类中的sqlSession的getMapper()方法的括号中,直接传接口类(sqlSession.getMapper(IPetDao.calss))后面可直接点出方法。
package com.mapper;
import com.pojo.Pet;
import java.util.List;
public interface IPetDao {
List<Pet> selectPets();
int update(Pet pet);
int insert(Pet pet);
int delete(int id);
}
3、PetMapper.xml
该类先构建模板,在后面添加相应数据库语句的标签如,代码同上方Mapper.xml
4、修改sqlMapConfig.xml中的相关配置,如包扫描和代理扫描
<typeAliases>
<typeAlias type="java.util.List" alias="list"/>
<package name="com.pojo"/>
typeAliases>
<mappers>
<mapper resource="mapper/PetMapper.xml"/>
mappers>
5、建立测试类
在该类中,使用org.junit包下的Test、After、Before方法,见下方详细代码。在Test中,sqlSession.getMapper(IPetDao.class).selectPets();方法传入的是接口类,getMapper方法中有类似于 Class c=IPetDao.class,IPetDao iPetDao = c.newInstance();iPetDao.selectPets()的方法。
package myBatis;
import com.mapper.IPetDao;
import com.pojo.Pet;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class Test1 {
private SqlSession sqlSession;
@Before
public void init(){
try {
//读取核心配置文件
InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
//找一个建筑者盖工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//构建工厂
SqlSessionFactory factory = builder.build(is);
//工厂生产SQLSession对象
sqlSession = factory.openSession();
//SQLSession对象代理增删改查
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void selectPets(){
List<Pet> pets = sqlSession.getMapper(IPetDao.class).selectPets();
System.out.println(pets);
}
@Test
public void update(){
Pet pet = new Pet();
pet.setPid(2);
pet.setPname("胖花花");
pet.setPbreed(1);
pet.setPsex(1);
pet.setPbirthday(new Date());
int i = sqlSession.getMapper(IPetDao.class).update(pet);
System.out.println(i);
}
@Test
public void insert(){
Pet pet = new Pet();
pet.setPname("肥花花");
pet.setPsex(1);
pet.setPbreed(1);
pet.setPbirthday(new Date());
int i = sqlSession.getMapper(IPetDao.class).insert(pet);
System.out.println(i);
}
@Test
public void delete(){
int i = sqlSession.getMapper(IPetDao.class).delete(5);
System.out.println(i);
}
@After
public void destroy(){
try{
sqlSession.commit();//提交
}catch (Exception e){
sqlSession.rollback();//回滚
}finally {
sqlSession.close();//关闭
}
}
}
@Before该注解在@Test之前执行,一般用于初始化或者执行@Test的重复代码
@Before
public void init(){
try {
//读取核心配置文件
InputStream is = Resources.getResourceAsStream("sqlMapConfig.xml");
//找一个建筑者盖工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//构建工厂
SqlSessionFactory factory = builder.build(is);
//工厂生产SQLSession对象
sqlSession = factory.openSession();
//SQLSession对象代理增删改查
} catch (IOException e) {
e.printStackTrace();
}
@Test相当于主方法
在该注解下自定义方法,一般写增删查改的实现代码
@Test
public void selectPets(){
List<Pet> pets = sqlSession.getMapper(IPetDao.class).selectPets();
System.out.println(pets);
}
@Test
public void update(){
Pet pet = new Pet();
pet.setPid(2);
pet.setPname("胖花花");
pet.setPbreed(1);
pet.setPsex(1);
pet.setPbirthday(new Date());
int i = sqlSession.getMapper(IPetDao.class).update(pet);
System.out.println(i);
}
@Test
public void insert(){
Pet pet = new Pet();
pet.setPname("肥花花");
pet.setPsex(1);
pet.setPbreed(1);
pet.setPbirthday(new Date());
int i = sqlSession.getMapper(IPetDao.class).insert(pet);
System.out.println(i);
}
@Test
public void delete(){
int i = sqlSession.getMapper(IPetDao.class).delete(5);
System.out.println(i);
}
@After该注解在@Test后执行,一般用于对提交的执行这里书写的是sqlSession会话的提交、回滚以及关闭的代码
@After
public void destroy(){
try{
sqlSession.commit();//提交
}catch (Exception e){
sqlSession.rollback();//回滚
}finally {
sqlSession.close();//关闭
}
}