当前位置: 首页 > >

Mybatis??动态SQL实现数据的增删改查

发布时间:

Mybatis之动态SQL实现数据的增删改查

一、所需jar包



二、XML文件配置























三、代码分析


测试类Test


public class Test {

public static void main(String[] args) throws Exception {
//指定XML文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//通过xml文件生成SqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//生成sqlSession
SqlSession sqlSession=sessionFactory.openSession();//默认为false,不会自动提交,需要调用commit()方法;和下面注释的这一行效果是一样的
//SqlSession sqlSession=sessionFactory.openSession(false);
IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);//为接口生成JDK动态代理对象

//删,通过id,批量删除向数据库中数据,注意:传到IStudentDao的数据不能直接就是一个字符串
List list = new ArrayList<>();
list.add("fbc6f519-cac5-4f53-8f37-284fc84d9215");
list.add("e4c6a8e5-7c1f-4ca8-b569-158c1912a0e5");
studentDao.batchDelete(list);

//增,向数据库中添加数据,对于sql语句,insert、delete、update如果返回值类型是boolean,只要执行成功就返回true,否则是false,如果返回值类型是int,返回sql语句作用的行数
System.out.println(studentDao.insert(new Student("cbc6f519-cac5-4f53-8f37-284fc84d9214","Mary","111","青岛")));

//改,修改数据
Student student = new Student();
student.setId("acd6a8e5-7c1f-4ca8-b569-158c1912a229");
student.setAddress("UK");
System.out.println(studentDao.update(student));

//查,查询数据库信息,并实现模糊查询
String name="大";
String mobile="3";
//sql映射文件无法对数据进行加工,计算,因此传到接口的数据要提前加工好
if(name!=null) {
name="%"+name+"%";
}
if(mobile!=null) {
mobile="%"+mobile+"%";
}
for (Student stu : studentDao.select(mobile, name)) {
System.out.println(stu.toString());//打印查询到的数据
}

sqlSession.commit();//提交sql语句
sqlSession.close();//释放资源
}
}

Student类


public class Student {

private String id;
private String name;
private String mobile;
private String address;

public Student() {

}

public Student(String id, String name, String mobile, String address) {
super();
this.id = id;
this.name = name;
this.mobile = mobile;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", mobile=" + mobile + ", address=" + address + "]";
}

}

Dao层代码


IStudentDao


/**
* 学生管理数据访问层
*
* @Snow
*/
public interface IStudentDao {

/**
* 实现添加数据抽象方法
*
* @Snow
*/
boolean insert(Student student);

/**
* 实现修改数据抽象方法
*
* @Snow
*/
boolean update(Student student);

/**
* 实现批量删除数据抽象方法
*
* @Snow
*/
int batchDelete(List list);

/**
* 实现查询及模糊查询抽象方法
*
* @Snow
*/
List select(@Param("mobile")String mobile,@Param("name")String name);
}

二、XML(SQL)映射文件配置








insert into student(id,user_name,mobile,address) value(#{id},#{mobile},#{name},#{address})




delete from student where id in(

#{id}

)




update student


user_name=#{name}
mobile=#{mobile}
address=#{address}

where id=#{id}





?



友情链接: