作为一个众多互联网后台开发工作者的一员,在日常工作中,常常利用一些现成的工具来解决一些实际的问题,但有一些工具只是局限于使用,而没有进一步了解。本系列blog是blog主希望加深对日常工作中经常使用的一些工具有进一步的了解。
要了解一个工具最最直接的方法是直接看这个工具的官网。http://www.mybatis.org/mybatis-3/zh/index.html
blog不想从复制一个mybatis官网,而是希望从自己的角度去理解,由于水平有限,过程中难免会参考一些网上的东西。
mybatis是个什么东西
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
知道了什么东西,不管三七二十一,先用起来
1.首先先安装个数据库,本人是在ubuntu下安装的mysql
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev
2.建表
create database test;
use test;
create table student
(
student_id int primary key auto_increment not null comment "学生id",
student_name varchar(20) not null comment "学生姓名",
student_age int not null comment "学生年龄",
student_phone varchar(20) not null comment "学生电话"
) charset=utf8 comment "学生信息表";
insert into student values(null, 'Jack', 20, '000000');
insert into student values(null, 'Mark', 21, '111111');
insert into student values(null, 'Lily', 22, '222222');
insert into student values(null, 'Lucy', 23, '333333');
3.添加config.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>
<typeAliases>
<typeAlias alias="Student" type="com.yongssu.mybatis.demo1.Student"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="student.xml"/>
</mappers>
</configuration>
4.声明一个java类,对应student表
public class Student {
private int student_id;
private String student_name;
private int student_age;
private String student_phone;
public Student() {
super();
}
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public int getStudent_age() {
return student_age;
}
public void setStudent_age(int student_age) {
this.student_age = student_age;
}
public String getStudent_phone() {
return student_phone;
}
public void setStudent_phone(String student_phone) {
this.student_phone = student_phone;
}
@Override
public String toString() {
return "Student{" +
"student_id=" + student_id +
", student_name='" + student_name + '\'' +
", student_age=" + student_age +
", student_phone='" + student_phone + '\'' +
'}';
}
}
5.配置student.xml文件
<?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.yongssu.mybatis.demo1.StudentMapper">
<select id="selectStudentById" parameterType="int" resultType="Student">
<![CDATA[
select * from student where student_id = #{id}
]]>
</select>
<select id="selectStudent" parameterType="int" resultType="Student">
<![CDATA[
select * from student where student_id = #{id}
]]>
</select>
</mapper>
6.声明StudentMapper类
public interface StudentMapper {
Student selectStudent(int id);
@Select("select * from student where student_id = #{id}")
Student selectStudent2(int id);
}
7.最终的测试类
public class MybatisTest {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
Reader reader = Resources.getResourceAsReader("config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
test01();
test02();
test03();
}
/**
* 通过xml配置实现sql查询
* @throws IOException
*/
public static void test01() throws IOException {
// 执行sql
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Student student = sqlSession.selectOne("com.yongssu.mybatis.demo1.StudentMapper.selectStudentById", 1);
System.out.println(student);
} finally {
sqlSession.close();
}
}
/**
* 通过接口+xml配置实现sql查询
* @throws IOException
*/
public static void test02() throws IOException {
// 执行sql
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.selectStudent(2);
System.out.println(student);
} finally {
session.close();
}
}
/**
* 通过注解实现sql查询
* @throws IOException
*/
public static void test03() throws IOException {
// 执行sql
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.selectStudent2(3);
System.out.println(student);
} finally {
session.close();
}
}
}
最后声明
记得引入mybatis jar包.运行之后应该能看到三条数据.