基于xml配置的入门案例
1、创建表
2、创建pojo类
3、编写数据库连接资源db.properties
4、编写 config.xml
5、编写My_StudentMap.xml
之后注册xml映射文件
6、编写dao层
7、编写test测试类
1、创建表
create table My_Student(
id number(10) primary key,
name varchar(10),
email varchar(30),
bod Date
);
2、创建pojo类
package com.briup.pojo;
import java.util.Date;
public class My_Student {
private Integer id;
private String name;
private String email;
private Date bod;
public My_Student() {
super();
}
public My_Student(Integer id, String name, String email, Date bod) {
super();
this.id = id;
this.name = name;
this.email = email;
this.bod = bod;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBod() {
return bod;
}
public void setBod(Date bod) {
this.bod = bod;
}
@Override
public String toString() {
return "My_Student [id=" + id + ", name=" + name + ", email=" + email + ", bod=" + bod + "]";
}
}
3、编写数据库连接资源db.properties
dirver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=briup
password=briup
4、编写 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>
<properties resource="db.properties"></properties><!-- 导入db.properties资源文件 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
</transactionManager><!-- JDBC/MANAGED -->
<dataSource type="POOLED"><!--unpooled不使用连接池/pooled使用连接池 -->
<property name="driver" value="${dirver}" /><!--数据库的dirver -->
<property name="url" value="${url}" /><!--连接数据库的url -->
<property name="username" value="${username}" /><!--数据库用户名 -->
<property name="password" value="${password}" /><!--对应用户名的密码 -->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/briup/maps/My_StudentMap.xml" /><!-- 写好map.xml后进行注册 -->
</mappers>
</configuration>
5、编写My_StudentMap.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.briup.pojo.My_Student">
<resultMap type="com.briup.pojo.My_Student" id="student">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="email" property="email" />
<result column="bod" property="bod" />
</resultMap>
<select id="findStudentById" parameterType="int" resultMap="student">
select * from My_Student where id=#{id}
</select>
<insert id="saveStudent" parameterType="com.briup.pojo.My_Student">
insert into My_Student
values(#{id},#{name},#{email},#{bod})
</insert>
<select id="getAllStudent" resultMap="student">
select * from My_Student
</select>
</mapper>
之后注册xml映射文件
6、编写dao层
package com.briup.dao;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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 com.briup.pojo.My_Student;
public class StudentDao {
static SqlSessionFactory factory = null;
static {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("conf.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
factory = builder.build(inputStream);
}
public My_Student getStudent(int id) {
SqlSession session = factory.openSession();
String statment = "com.briup.pojo.My_Student.findStudentById"; //字符串 xml的namespace.id
My_Student my_Student = session.selectOne(statment, id);
session.commit();
session.close();
return my_Student;
}
public void saveStudent(My_Student student) {
SqlSession session = factory.openSession();
String statment = "com.briup.pojo.My_Student.saveStudent";
session.insert(statment, student);
session.commit();
session.close();
}
public List<My_Student> getAll() {
List<My_Student> list = new ArrayList<>();
SqlSession session = factory.openSession();
String statment = "com.briup.pojo.My_Student.getAllStudent";
list = session.selectList(statment);
session.commit();
session.close();
return list;
}
}
7、编写test测试类
package com.briup.Servers;
import java.sql.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import com.briup.dao.StudentDao;
import com.briup.pojo.My_Student;
public class TestStudent {
public StudentDao studentdao;
@Before
public void init() {
studentdao = new StudentDao();
}
@Test
public void saveStudent() {
My_Student my_Student = new My_Student(1, "tom", "briup@123.com", new Date(new java.util.Date().getTime()));
studentdao.saveStudent(my_Student);
}
@Test
public void selectStudentbyid() {
My_Student my_Student = studentdao.getStudent(1);
System.out.println(my_Student);
}
@Test
public void selectAllStudent() {
List<My_Student> list = studentdao.getAll();
for (My_Student my_Student : list) {
System.out.println(my_Student);
}
}
}