MyBatis 实现 增删改查
今天来实现 mybatis 最基本的增删改查操作:
依旧是 三步:1.config.xml 配置文件 2. mapper.xml 映射配置文件 3.测试
首先1.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>
<!--开发环境 (自己的计算机) -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!--POOLED:使用连接池 避免打开和关闭数据库的耗时 -->
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="sys as sysdba" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载映射 文件 -->
<mapper resource="org/cjr/entity/PersonMapper.xml" />
</mappers>
</configuration>
这个配置文件是固定写法,大部分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="org.cjr.entity.PersonMapper">
<select id="queryPersonById" resultType="org.cjr.entity.Person" parameterType="int">
select * from Person where id = #{id}
</select>
<select id="queryAll" resultType="org.cjr.entity.Person">
select * from Person
</select>
<insert id="addPerson" parameterType="org.cjr.entity.Person">
insert into person values(#{id},#{name},#{age})
</insert>
<delete id="deletePerson" parameterType="int">
delete from person where id= #{id}
</delete>
<update id="updatePerson" parameterType="org.cjr.entity.Person">
update person set name=#{name},age=#{age} where id=#{id}
</update>
</mapper>
该映射文件 存放了Sql语句 以及 返回值类型等 信息。
最后我们写测试类
package org.cjr.entity;
import java.io.IOException;
import java.io.Reader;
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;
public class testMyBatis {
public static void queryStudentBySno() throws IOException {
//加载配置文件
Reader reader = Resources.getResourceAsReader("config.xml");
//qlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//session- 相当于connection
SqlSession session = sqlSessionFactory.openSession();
//映射文件中的namespace + id
Person person = session.selectOne("org.cjr.entity.PersonMapper.queryPersonById",1);
// Person person = (Person) session.selectOne("org.cjr.entity.PersonMapper.queryAll");
System.err.println(person);
session.close();
}
//查询
public static void queryAllStudent() throws IOException {
//加载配置文件
Reader reader = Resources.getResourceAsReader("config.xml");
//qlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//session- 相当于connection
SqlSession session = sqlSessionFactory.openSession();
//映射文件中的namespace + id
List<Person> persons = session.selectList("org.cjr.entity.PersonMapper.queryAll");
System.err.println(persons);
session.close();
}
//增加
public static void addStudent() throws IOException {
//加载配置文件
Reader reader = Resources.getResourceAsReader("config.xml");
//qlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//session- 相当于connection
SqlSession session = sqlSessionFactory.openSession();
//映射文件中的namespace + id
Person person = new Person(3,"ww",30);
int count = session.insert("org.cjr.entity.PersonMapper.addPerson",person);
session.commit();//提交事务
System.out.println("增加"+count+"个学生");
session.close();
}
//删除
public static void deleteStudent() throws IOException {
//加载配置文件
Reader reader = Resources.getResourceAsReader("config.xml");
//qlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//session- 相当于connection
SqlSession session = sqlSessionFactory.openSession();
//映射文件中的namespace + id
int count = session.delete("org.cjr.entity.PersonMapper.deletePerson",3);
session.commit();//提交事务
System.out.println("删除"+count+"个学生");
session.close();
}
//改
public static void updateStudent() throws IOException {
//加载配置文件
Reader reader = Resources.getResourceAsReader("config.xml");
//qlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//session- 相当于connection
SqlSession session = sqlSessionFactory.openSession();
//映射文件中的namespace + id
Person person = new Person();
person.setId(2);
person.setName("zzz");
person.setAge(99);
int count = session.update("org.cjr.entity.PersonMapper.updatePerson",person);
session.commit();//提交事务
System.out.println("修改"+count+"个学生");
session.close();
}
public static void main(String[] args) throws IOException {
//deleteStudent();
//updateStudent();
//addStudent();
queryAllStudent();
}
}
这些方法中 都是通过session对象来 调用 增删改查的方法,相比较 传统的Jdbc方式,mybatis解决了 Sql与代码之间的耦合问题。
还没有评论,来说两句吧...