(1)编写需要CRUD操作的实体类Student
public class Student {
private String id;
private String name;
private String age;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student(String id, String name, String age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Student() {
super();
}
}
(2)配置用于编写SQL语句的StudentMapper文件(路径com/jyk/mybatis/crud/StudentMapper.xml),并将mapper文件路径以及实体类别名加入到mybatis总配置文件中
<?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.jyk.mybatis.crud.Student">
<!-- 当实体属性名与表字段名不一样时,以下代码必须书写
当实体属性名与表字段名一样时,以下代码可选 -->
<resultMap type="crudstudent" id="studentMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
</resultMap>
<!-- 添加操作 -->
<insert id="add" parameterType="crudstudent">
insert into student(id,name,age,sex) values(#{id},#{name},#{age},#{sex})
</insert>
<!--
根据id查询操作:
如果参数不是一个实体,只是一个普通参数,例如int,double,string
这里的#{中间的变量名可以随便写},不过建议用方法的形式参数
-->
<select id="findById" parameterType="int" resultType="crudstudent">
select id,name,age,sex from student where id = #{id}
</select>
<!-- 查询所有操作 -->
<select id="findall" resultType="crudstudent">
select id,name,age,sex from student
</select>
<!-- 更新操作 -->
<update id="update" parameterType="crudstudent">
update student set name=#{name},age=#{age},sex=#{sex} where id=#{id}
</update>
<!-- 删除操作 -->
<delete id="delete" parameterType="crudstudent">
delete from student where id=#{id}
</delete>
<!--
增删改查注意事项 :insert/update/delete标签只是一个模板,在做操作时,其实是以sql语句为核心的
即当做增/删/改时,insert/update/delete标签可通用,但是提倡做什么操作就使用什么标签
但做查询时只能使用select标签
-->
<!-- mybatis会将查询出来的表记录和resultMap值对应的映射结果互相匹配 -->
<select id="findById" parameterType="int" resultMap="studentMap">
select id,name,age,sex from student where id = #{id}
</select>
</mapper>
<?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>
<!-- 设置类型别名 -->
<typeAliases>
<typeAlias type="com.jyk.mybatis.crud.Student" alias="crudstudent"/>
<!-- <typeAlias type="com.jyk.mybatis.page.Student" alias="pagestudent"/>
<typeAlias type="com.jyk.mybatis.dynamic.Student" alias="dynamicstudent"/> -->
</typeAliases>
<!-- 设置一个默认的连接环境信息 -->
<environments default="mysql_env">
<!-- 连接环境信息,取一个唯一的编号 -->
<environment id="mysql_env">
<!-- mybatis使用的jdbc事务管理方式 -->
<transactionManager type="jdbc">
</transactionManager>
<!-- mybatis使用连接池方式来获取链接 -->
<dataSource type="pooled">
<!-- 配置与数据库交互的四个属性 -->
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/jyk/mybatis/crud/StudentMapper.xml"/>
<!-- <mapper resource="com/jyk/mybatis/page/StudentMapper.xml"/>
<mapper resource="com/jyk/mybatis/dynamic/StudentMapper.xml"/> -->
</mappers>
</configuration>
(3)编写DAO,通过Java代码控制Mybatis进行增删改查
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.jyk.mybatis.util.MyBatisUtil;
public class StudentDao {
/*
* 增加的方法1
*/
public void add(Student stu)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession();
int i = sqlSession.insert(Student.class.getName()+".add", stu);
System.out.println("本次操作影响了"+i+"行数据");
//事务提交
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
//事务回滚
sqlSession.rollback();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}
/*
* 根据ID查找
*/
public Student findById(int id)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession();
Student stu = sqlSession.selectOne(Student.class.getName()+".findById", id);
return stu;
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}
/*
* 查询所有学生
*/
public List<Student> findall()
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession();
return sqlSession.selectList(Student.class.getName()+".findall");
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}
/*
* 更新学生信息
*/
public void update(Student stu)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession();
sqlSession.update(Student.class.getName()+".update", stu);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}
/*
* 删除操作
*/
public void delete(Student stu)
{
SqlSession sqlSession = null;
try{
sqlSession = MyBatisUtil.getSqlSession();
sqlSession.update(Student.class.getName()+".delete", stu);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
MyBatisUtil.closeSqlSession();
}
}
}
上面增删改查操作案例需要关心的几个地方:
(1)mapper文件的名称需要唯一,所以一般以操作的实体类全路径为名称空间
(2)当实体属性名与表字段名不一样时,resultMap必须书写,当实体属性名与表字段名一样时,resultMap可选,resultMap的type代表参数的类型,可供parameterType,resultType直接名称引用,id作为唯一标识,可被resultMap使用
(3)如果参数不是一个实体,只是一个普通参数,例如int,double,string,这里的#{中间的变量名可以随便写},不过建议用方法的形式参数
<select id="findById" parameterType="int" resultType="crudstudent">
select id,name,age,sex from student where id = #{id}
</select>
(4)增删改查注意事项 :insert/update/delete标签只是一个模板,在做操作时,其实是以sql语句为核心的,即当做增/删/改时,insert/update/delete标签可通用,但是提倡做什么操作就使用什么标签,但做查询时只能使用select标签
(5)parameterType:指入参类型,可引用resultMap的type属性
resultType:指出参类型,可引用resultMap的id属性
resultMap:mybatis会将查询出来的表记录和resultMap值对应的映射结果互相匹配