學習Mybatis這么多天,那么我給大家分享一下我的學習成果。從最基礎的開始配置。
一、創建一個web項目,看一下項目架構
二、說道項目就會想到需要什么jar
三、就是准備大配置鏈接Orcl數據庫
<?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> <!-- 別名的定制 --> <typeAliases> <!-- 按類型名定制別名 --> <!-- <typeAlias type="cn.happy.entity.Student" alias="Student"/> --> <!-- 拿當前指定包下的簡單類名作為別名 --> <package name="cn.happy.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <!-- 使用jdbc的事務 --> <transactionManager type="JDBC" /> <!-- 使用自帶的連接池 --> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="T2" /> <property name="password" value="T2" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/happy/dao/StudentDAO.xml" /> </mappers> </configuration>
四、准備小配置
<?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="cn.happy.dao.IStudentDAO">
//增刪改 </mapper>
五、准備工具類
package cn.happy.util; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * 工具類 * @author Happy * */ public class MybatisUtil { private static String config="mybatis-config.xml"; static Reader reader; static{ try { reader= Resources.getResourceAsReader(config); } catch (IOException e) { e.printStackTrace(); } } private static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); //提供一個可以獲取到session的方法 public static SqlSession getSession() throws IOException{ System.out.println("22222"+factory); //弊病,就是工廠是 // 1.1 openSession到底做了什么 SqlSession session = factory.openSession(); System.out.println("3333"); return session; } }
六、准備實體類
package cn.happy.entity; import java.util.Date; /** * 學生實體類 * @author Happy * */ public class Student { private Integer stuno; private String stuname; private Integer stuage; private Date studate; public String toString() { return "Student [stuno=" + stuno + ", stuname=" + stuname + ", stuage=" + stuage + ", studate=" + studate + "]"; } public Integer getStuno() { return stuno; } public void setStuno(Integer stuno) { this.stuno = stuno; } public String getStuname() { return stuname; } public void setStuname(String stuname) { this.stuname = stuname; } public Integer getStuage() { return stuage; } public void setStuage(Integer stuage) { this.stuage = stuage; } public Date getStudate() { return studate; } public void setStudate(Date studate) { this.studate = studate; } }
七、下面是具體的代碼增刪改查前面是公用的
(一)下面實現查看所有
1在接口定義方法
//查詢所有記錄 public List<Student> findAll() throws IOException;
2在配置中=======>由於動態加載所以不需要實現類
<!-- 查詢所有 --> <select id="findAll" resultType="Student"> select * from student </select>
3測試類中
IStudentDAO dao; @Before public void initData() throws IOException{ SqlSession session = MybatisUtil.getSession(); //動態踢出實現類 //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO"> dao=session.getMapper(IStudentDAO.class); } /** * selectALl學生 * @throws IOException */ @Test public void findAll() throws IOException{ List<Student> list = dao.findAll(); for (Student student : list) { System.out.println(student.getStuname()); } }
(二)下面是添加
1在接口定義方法
//添加 public int addStu(Student stu) throws IOException;
2在配置中=======>由於動態加載所以不需要實現類
<insert id="insertStudent" parameterType="Student" > insert into student(stuname,stuage,studate) values(#{stuname},#{stuage},#{studate}) <!-- sqlserver 和Mysql 只有自自增 自增時機和insert時機: 先insert返回自增值 Oracle先產生一個自增值,然后再執行insert --> <selectKey keyProperty="stuno" resultType="int"> <!-- Mysql的 --> select @@identity <!-- orcl的 --> select sql_num.currval from dual </selectKey> </insert>
3測試類中
@Test public void testAdd() throws IOException{ Student stu=new Student(); stu.setStuname("Y2161好人"); stu.setStuage(21); stu.setStudate(new Date()); System.out.println("添加前======="+stu); IStudentDAO dao=new StudentDAOImpl(); dao.addStu(stu); System.out.println("添加后======="+stu); }
(二)下面是刪除
1在接口定義方法
//刪除 public int delStu(int id) throws IOException;
2在配置中=======>由於動態加載所以不需要實現類
<!--刪除學生 --> <delete id="delStudent"> delete from student where stuno=#{xxx}<!-- #{xxx}隨便寫,起到一個占位的作用 --> </delete>
3測試類中
/** * 刪除學生 * @throws IOException */ @Test public void delStudent() throws IOException{ dao.delStu(2); System.out.println("ok"); }
(二)下面是模糊查詢
1在接口定義方法
//按照學生姓名查詢學生集合(一個是實體一個是字符串其實都可以) public List<Student> findStudntByName(Student stu) throws IOException; public List<Student> findStudntByName(String stuname) throws IOException;
2在配置中=======>由於動態加載所以不需要實現類
<!--模糊查詢 mysql的數據庫 -->
<select id="findStudentByName" resultType="Student">
<!-- select * from student where stuname like concat('%',#{stuname},'%') -->
select * from student where stuname like '%${value}%'
</select>
3測試類中
/** * 模糊查詢 * @throws IOException */ @Test public void findStudentByName() throws IOException{ /*Student stu=new Student(); stu.setStuname("人");*/ List<Student> list = dao.findStudntByName("人"); for (Student student : list) { System.out.println(student.getStuname()); } }