MyBatis:支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Ordinary Java Objects,普通的 Java对象)映射成数据库中的记录。
在这里简单的创建一个java web项目MyBatis:
在这里,StudentMapper.java相当于dao层,而StudentMapper.xml相当于dao的impl。
实体类Student.java:
package cn.hbue.entity;
public class Student {
private Integer id;
private String name;
private String sex;
private String birth;
private String department;
private String address;
public Student(){
super();
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", birth="
+ birth + ", department=" + department + ", address=" + address
+ "]";
}
public Student(Integer id,String name,String sex,String birth,String department,String address){
super();
this.id=id;
this.name=name;
this.sex=sex;
this.birth=birth;
this.department=department;
this.address=address;
}
}
package cn.hbue.mapper;
import java.util.List;
import cn.hbue.entity.Student;
/**接口类型 相当于StuedntDao **/
public interface StudentMapper {
public Student getStudentById(Integer id); //通过id查询
public int saveStudent(Student stu); //插入数据
public void updateStudent(Student stu); //更新数据
public void deleteStudentById(Integer id); //删除数据
//批量操作
public List<Student> getAllStudent();
}
StudentMapper.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">
<!-- 相当于 StudentDaoImpl
namespace:指向接口,对应接口的全类名,一个命名空间对应一个dao接口-->
<mapper namespace="cn.hbue.mapper.StudentMapper">
<!-- id:对应接口中的方法 resultType:对应返回值类型 parameterType: 对应参数类型-->
<select id="getStudentById" resultType="cn.hbue.entity.Student">
select * from student where id = #{id}
</select>
<insert id="saveStudent" parameterType="cn.hbue.entity.Student">
insert into student values(#{id},#{name},#{sex},#{birth},#{department},#{address})
</insert>
<update id="updateStudent" parameterType="cn.hbue.entity.Student" >
update student set id=#{id}, name=#{name},sex=#{sex},birth=#{birth},department=#{department},address=#{address} where id=#{id}
</update>
<delete id="deleteStudentById" parameterType="Integer">
delete from student where id=#{id}
</delete>
<!-- 批量操作 -->
<resultMap type="cn.hbue.entity.Student" id="StudentList">
<!-- id:数据库表主键 , property:对应javabean的属性 ,column:对应数据库表的字段 -->
<id property="idCard" column="stu_id_card"/>
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="birth" column="birth"/>
<result property="department" column="department"/>
<result property="address" column="address"/>
</resultMap>
<select id="getAllStudent" resultMap="StudentList">
select * from student;
</select>
</mapper>
mybatis的映射文件mybatis-configure.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/lanou?useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="cn/hbue/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
在这里,针对数据表student做了简单的增删改查以及批量的查询操作:
package cn.hbue.test;
import java.io.IOException;
import java.io.InputStream;
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 org.junit.Test;
import cn.hbue.entity.Student;
import cn.hbue.mapper.StudentMapper;
public class MybatisTest {
@Test
public static void main(String[] args) throws IOException{
String resource ="mybatis-configure.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
// Student stu_select = mapper.getStudentById(907); //查询id=907
// Student stu_insert = new Student(909,"幺妹儿","女","1992","舞蹈系","鄂州市鄂城区");
// mapper.saveStudent(stu_insert); //将数据插入数据库
// System.out.println("查询907:"+stu_select.toString());
// Student stu = mapper.getStudentById(909); //查询刚插入的数据 id=908
// System.out.println("查询908:"+stu.toString());
// System.out.println("更新之后查询908:"+stu.toString());
// Student stu_update = new Student(909,"哈哈","女","1999","舞蹈系","鄂州市鄂城区");
// mapper.updateStudent(stu_update);
// System.out.println("更新之后查询909: "+stu_update.toString());
// mapper.deleteStudentById(908); //删除成功
// System.out.println("删除成功-----");
List<Student> list=mapper.getAllStudent();
for(Student student: list){
System.out.println("学生信息:"+student.toString());
}
System.out.println("查询所有student信息成功-----");
session.commit();
}
finally {
session.close();
}
}
}