总体大概
1 - 添加jar包(依赖)
1.mybatis包
2.mysql-connector-java
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guard</groupId>
<artifactId>Test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
<build>
<finalName>Test</finalName>
<!--由于在Idea中我把java包设置成Sources Root,所以扫描不到xml文件,要设置一下,能扫描到xml文件-->
<!--项目文件下面的-->
<resources>
<resource>
<directory>src/main/java</directory>
<!--java下面的某某文件夹下面的某某xml文件-->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
2 建表(我就在数据库里建了)
我建了个student表
sid 主键,自动增长
sname
sage
3- 写student表的实体类
get和set,还有tostring方法
package com.guard.entity;
public class Student {
private int sid;
private String sname;
private int sage;
public Student() {
}
public Student(int sid, String sname, int sage) {
this.sid = sid;
this.sname = sname;
this.sage = sage;
}
public Student(String sname, int sage) {
this.sname = sname;
this.sage = sage;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", sage=" + sage +
'}';
}
}
4- 添加配置文件
<?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>
<!--连接数据库-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/1203"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="com/guard/entity/StudentMybatis.xml"/>
</mappers>
</configuration>
5- 写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="student">
<select id="getAll" resultType="com.guard.entity.Student">
select * from student
</select>
<select id="getInsert" parameterType="com.guard.entity.Student">
INSERT INTO student (sname,sage) VALUES (#{sname} , #{sage})
</select>
<select id="getDelete" parameterType="com.guard.entity.Student">
delete from student where sid=#{sid}
</select>
<select id="getUpdate" parameterType="com.guard.entity.Student">
update student set sname=#{sname} where sid=#{sid}
</select>
</mapper>
6- 测试(单元测试)
import com.guard.entity.Student;
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 java.util.List;
public class Test {
@org.junit.Test
public void test(){
try {
/*写个sqlSessionFactory工厂来加载配置文件*/
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession();
/*查看所有*/
// List<Student> students = sqlSession.selectList("student.getAll");
// for (Student student : students) {
// System.out.println(student);
// }
/*增加*/
// sqlSession.insert("student.getInsert",new Student("wokao",22));
/*删除*/
// sqlSession.delete("student.getDelete",4);
/*修改*/
sqlSession.update("student.getUpdate",new Student(3,"woyu",22));
sqlSession.commit();
sqlSession.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
查询所有
添加前
添加成功后
注解
1 - 写个接口
package com.guard.entity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface StudentAnnotation {
@Insert("insert into student (sname,sage) values(#{sname},#{sage})")
public void getInsert(Student student);
@Delete("delete from student where sid=#{sid}")
public void getDelete(int sid);
@Update("update student set sname=#{sname},sage=#{sage} where sid=#{sid}")
public void getUpdate(Student student);
@Select("select * from student")
public List<Student> getSelect();
}
2 - mybatis-config.xml中一个class来加载StudentAnnotation接口
3 - 测试
import com.guard.entity.Student;
import com.guard.entity.StudentAnnotation;
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.util.List;
public class TestAnnotation {
private SqlSession sqlSession;
/*@Before是在@Test之前执行*/
@Before
public void before(){
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
sqlSession = sqlSessionFactory.openSession();
}catch (Exception e){
e.printStackTrace();
}
}
@Test
public void test(){
StudentAnnotation annotation = sqlSession.getMapper(StudentAnnotation.class);
List<Student> students = annotation.getSelect();
for (Student student : students) {
System.out.println(student);
}
}
/*@After在@Test之后执行*/
@After
public void after(){
sqlSession.commit();
sqlSession.close();
}
}
查询成功
剩下的增删改是一样的写法