01.书写大配置:
mybatis-config.xml,并将大配置放在src目录下:
<?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"> <!-- root Node --> <configuration> <!--别名定制--> <typeAliases> <typeAlias type="cn.happy.entity.Dept" alias="Dept"/> </typeAliases> <environments default="development"> <!-- only One db environment --> <environment id="development"> <!-- tx use jdbc's --> <transactionManager type="managed" /> <!-- datasource's type is pooled it can use the value of list: pooled unpooled jndi --> <dataSource type="UNPOOLED"> <property name="driver" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" ></property> <property name="username" value="dog" /> <property name="password" value="dog" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/happy/entity/Dept.xml" /> <mapper resource="cn/happy/entity/GetMapper.xml" /> </mappers> </configuration>
02.书写实体类:
package cn.happy.entity; public class Dept { private Integer deptno; private String deptname; public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } }
03.书写小配置(包含crud)
<?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"> <!-- namespace:隔离同名类 --> <mapper namespace="cn.happy.entity.Dept"> <select id="selectDept" resultType="Dept"> <!-- SQL 列名,表名和数据库表 不区分大小写 记录是区分的: oa OA --> select * from Dept </select> <!-- 1.1 带条件查询 --> <select id="selectDeptById" parameterType="Dept" resultType="Dept"> select * from Dept where deptno=#{deptno} </select> <!-- 1.2 insert --> <insert id="insertDept" parameterType="Dept"> insert into Dept(deptno,deptname) values(#{deptno},#{deptname}) </insert> <!-- 1.2 update --> <update id="updateDeptById" parameterType="Dept"> update Dept set deptname=#{deptname} where deptno=#{deptno} </update> <!-- 1.3 delete--> <delete id="deleteDeptById" parameterType="Dept"> delete from Dept where deptno=#{deptno} </delete> </mapper>
04.测试类
package cn.happy.test; //ALt+/ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Reader; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import cn.happy.dao.IDeptDao; import cn.happy.entity.Dept; public class H_01Test { /** * 1.5 getMapper() * @throws FileNotFoundException * @throws Exception */ @Test public void testgetAllDepts() throws Exception{ //通过session的getMapper(传入接口类型的字节码)返回的是一个接口类型的对象 IDeptDao mapper = session.getMapper(IDeptDao.class); //通过接口类型的对象中自己定制的方法,可以得到想要的结果 try { List<Dept> list = mapper.getAllDepts(); for (Dept dept : list) { System.out.println(dept.getDeptname()); } } catch (Exception e) { e.printStackTrace(); } } /** * 1.1 delete * @throws FileNotFoundException * @throws Exception */ @Test public void testdeleteDeptById() throws Exception{ Dept dt=new Dept(); dt.setDeptno(32); int count = session.delete("deleteDeptById",dt); session.commit(); System.out.println(count); session.close(); } /** * 1.1 update * @throws FileNotFoundException * @throws Exception */ @Test public void testupdateDeptById() throws Exception{ Dept dt=new Dept(); dt.setDeptno(1); dt.setDeptname("月饼不"); int count = session.update("updateDeptById",dt); session.commit(); System.out.println(count); session.close(); } /** * 1.1 insert * @throws FileNotFoundException * @throws Exception */ @Test public void testinsertDept() throws Exception{ Dept dt=new Dept(); dt.setDeptname("月饼兔子"); dt.setDeptno(54); int count = session.insert("insertDept",dt); session.commit(); System.out.println(count); session.close(); } /** * 1.1 带条件查询 * @throws FileNotFoundException * @throws Exception */ @Test public void testselectDeptById() throws Exception{ Dept dt=new Dept(); dt.setDeptno(1); Dept dept = session.selectOne("selectDeptById",dt); System.out.println(dept.getDeptname()); session.close(); } SqlSession session; @Before public void initData() throws FileNotFoundException{ //1.1 SqlSessionFactoryBuilder SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //1.2 SqlSesssionFactory Reader reader=new FileReader("src/mybatis-config.xml"); SqlSessionFactory factory = builder.build(reader); //1.3 SqlSession session= factory.openSession(); } @Test public void testselectDept() throws Exception{ //1.1 SqlSessionFactoryBuilder SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //1.2 SqlSesssionFactory Reader reader=new FileReader("src/mybatis-config.xml"); SqlSessionFactory factory = builder.build(reader); //1.3 SqlSession SqlSession session = factory.openSession(); List<Dept> list = session.selectList("selectDept"); for (Dept dept : list) { System.out.println(dept.getDeptname()); } session.close(); } }
别名的使用(如下实体类就可以用Dept代替)
<!--别名定制--> <typeAliases> <typeAlias type="cn.happy.entity.Dept" alias="Dept"/> </typeAliases>
GetMapper()的使用
首先创建接口:IDeptDao
package cn.happy.dao; import java.util.List; import cn.happy.entity.Dept; /** * 接口 * @author Happy * 该接口有对应的xml文件 * */ public interface IDeptDao { //部门中的数据进行CRUD操作 //检索所有部门 public List<Dept> getAllDepts(); }
然后创建该接口对应的xml文件,相当于把实体类里边的方法描述在了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">
<!-- 相当与getMapper的映射文件 mapper里边有命名空间-->
<mapper namespace="cn.happy.dao.IDeptDao">
<select id="getAllDepts" resultType="Dept">
select * from dept
</select>
</mapper>
书写测试类:
/** * 1.5 getMapper() * @throws FileNotFoundException * @throws Exception */ @Test public void testgetAllDepts() throws Exception{ //通过session的getMapper(传入接口类型的字节码)返回的是一个接口类型的对象 IDeptDao mapper = session.getMapper(IDeptDao.class); //通过接口类型的对象中自己定制的方法,可以得到想要的结果 List<Dept> list = mapper.getAllDepts(); for (Dept dept : list) { System.out.println(dept.getDeptname()); } }
/** * 1.5 getMapper() * @throws FileNotFoundException * @throws Exception */ @Test public void testgetAllDepts() throws Exception{ //通过session的getMapper(传入接口类型的字节码)返回的是一个接口类型的对象 IDeptDao mapper = session.getMapper(IDeptDao.class); //通过接口类型的对象中自己定制的方法,可以得到想要的结果 List<Dept> list = mapper.getAllDepts(); for (Dept dept : list) { System.out.println(dept.getDeptname()); } }