首先下载MyBatis jar包, 可以去MyBatis中文官网下载
项目中导入MyBatis jar包和JDBC jar包(此处用的MySQL)
新建conf.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>
<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/yourDatabase"/>
<property name="username" value="root"/>
<property name="password" value="yourPassword"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="下面配置文件的路径, 包名全称/???.xml"/>
</mappers>
</configuration>
配置好后写个Student类测试:
1 public class Student { 2 int id; 3 String name; 4 int age; 5 String sex; 6 7 public int getId() { 8 return id; 9 } 10 11 public void setId(int id) { 12 this.id = id; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 public String getSex() { 32 return sex; 33 } 34 35 public void setSex(String sex) { 36 this.sex = sex; 37 } 38 39 @Override 40 public String toString() { 41 return "Student{" + 42 "id=" + id + 43 ", name='" + name + '\'' + 44 ", age=" + age + 45 ", sex='" + sex + '\'' + 46 '}'; 47 } 48 }
带着toString()一会print方便
同时在数据库中创建student表,并插入几条数据:
1 CREATE TABLE student 2 ( 3 id int PRIMARY KEY, 4 name VARCHAR(20), 5 age INT, 6 sex VARCHAR(4) 7 ) 8 insert into student values(1, '张三', 20, '女'); 9 insert into student values(2, '李四', 21, '女'); 10 insert into student values(3, '王五', 22, '男'); 11 insert into student values(4, '马六', 23, '男');
配置Mapper.xml文件(上方Mapper resource写此文件)
<?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="定位到此的标识符"> <!-- id为定位到语句的唯一标识符, parameterType为输入参数类型, 只能有一个, 可以用自定义类,returnType-为返回值类型, 也只能有一个, 注意,这两个如果是自定义类需要包名+类名 -> <select id="getOneStudent" parameterType="int" resultType="Student"> select * from student where id=#{id} </select> <select id="getAllStudents" resultType="Student"> select * from student </select> </mapper>
用Mapper方式, 定义接口:
1 import java.util.List; 2 3 public interface StudentMapper { 4 public Student getOneStudent(int id); 5 public List<Student> getAllStudents(); 6 }
下面编写测试类:
1 import org.apache.ibatis.io.Resources; 2 import org.apache.ibatis.session.SqlSession; 3 import org.apache.ibatis.session.SqlSessionFactory; 4 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 5 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.util.List; 9 10 public class Test { 11 public static void main(String[] args) throws IOException { 12 String resource = "conf.xml"; 13 InputStream inputStream = Resources.getResourceAsStream(resource); 14 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 15 SqlSession session = sqlSessionFactory.openSession(); 16 StudentMapper studentMapper = session.getMapper(StudentMapper.class); 17 Student s = studentMapper.getOneStudent(1); 18 List<Student> arrayStudents = studentMapper.getAllStudents(); 19 System.out.println(s); 20 System.out.println(arrayStudents); 21 }
测试成功: