1、导入依赖
点击查看代码
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
点击查看代码
<?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>
<!--DB的四大步骤-->
<properties>
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/class?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property value="${driver}" name="driver"/>
<property value="${url}" name="url"/>
<property value="${username}" name="username"/>
<property value="${password}" name="password"/>
</dataSource>
</environment>
</environments>
<!--导入mapper-->
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
<!-- <mapper resource="mapper/TableMapper.xml" />-->
</mappers>
</configuration>
点击查看代码
package com.bh.po;
public class Student {
private int id;
private String name;
public Student() {
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
点击查看代码
package com.bh.dao;
import com.bh.po.Student;
import java.util.List;
public interface IStudentDAO {
public List<Student> findAll();
public int save(Student student);
public int remove(Student student);
public int modify(Student student);
}
点击查看代码
<?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.bh.dao.IStudentDAO">
<!--resultType为返回的结果值类型-->
<select resultType="com.bh.po.Student" id="findAll">
select * from student
</select>
<!--parameterType为传的参数类型-->
<insert id="save" parameterType="com.bh.po.Student">
insert into student values(#{id}, #{name})
</insert>
<!-- <delete id="">-->
<delete id="remove" parameterType="com.bh.po.Student">
delete from student where id=#{id}
</delete>
<update id="modify" parameterType="com.bh.po.Student">
update student set name=#{name} where id=#{id}
</update>
<!-- <update id=""></update>-->
</mapper>
点击查看代码
package com.bh.Test;
import com.bh.dao.IStudentDAO;
import com.bh.po.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
public class Test {
public static void main(String[] args) {
System.out.println("start--------");
//读取配置文件
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis.xml");
//获取工厂的builder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//创建工厂
SqlSessionFactory sqlSessionFactory = builder.build(in);
//创建mybatis的sqlSqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
IStudentDAO studentDAO = sqlSession.getMapper(IStudentDAO.class);
/* List<Student> all = studentDAO.findAll();
for (int i = 0; i < all.size(); i++) {
Student stu = all.get(i);
System.out.println(stu.getId() + "_" + stu.getName());
}*/
Student stu = new Student();
stu.setName("kuan");
stu.setId(23);
/*studentDAO.save(stu);*/
/*studentDAO.modify(stu);*/
studentDAO.remove(stu);
sqlSession.commit();
System.out.println("end---------");
}
}