MyBatis(百度百科):java
下面咱們來作第一個入門案例:sql
架構:apache
jar包:session
咱們建立一個學生實體類mybatis
package cn.entity; /** * 學生實體類 * @author hyj * */ public class Student { private Integer id;//編號 private Integer age;//年齡 private String name;//姓名 public Student() { } public Student(Integer id, Integer age, String name) { super(); this.id = id; this.age = age; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", age=" + age + ", name=" + name + "]"; } }
配置DAO和DAOImpl實現類架構
package cn.dao; import java.io.IOException; import java.util.List; import cn.entity.Student; /** * 學生的dao * @author hyj * */ public interface StudentDao { /** * 保存學生 * @param stu 學生對象 * @return 受影響行數 * @throws IOException */ int save(Student stu) throws IOException; /** * 根據id刪除學生信息 * @param id * @return */ int delete(Integer id); /** * 查看學生列表 * @return */ List<Student> studentList(); /** * 根據學生姓名模糊查詢學生信息 * @param name * @return */ List<Student> byNameList(String name); /** * 根據學生對象模糊查詢學習姓名 * @param stu * @return */ List<Student> byNameList(Student stu); /** * 更新學生信息 * @param stu * @return */ int updateStudent(Student stu); }
package cn.dao.impl; 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; import org.junit.Before; import cn.dao.StudentDao; import cn.entity.Student; import cn.util.SqlSessionUtil; /** * 學生的dao實現類 * * @author hyj * */ public class StudenDaoImpl implements StudentDao { SqlSession session = SqlSessionUtil.getSqlSession(); /** * 添加學生信息 */ @Override public int save(Student stu) throws IOException { int result = session.insert("insertStudent", stu); // 提交事務 session.commit(); SqlSessionUtil.closeSqlSession(); return result; } /** * 根據id刪除學生信息 */ @Override public int delete(Integer id) { int result = session.delete("deletStudent", id); session.commit(); SqlSessionUtil.closeSqlSession(); return result; } /** * 查詢學生列表 * * @return */ @Override public List<Student> studentList() { List<Student> students = session.selectList("studentlist"); SqlSessionUtil.closeSqlSession(); return students; } /** * 根據學生姓名模糊查詢學生列表:傳入的參數是普通字符串 */ @Override public List<Student> byNameList(String name) { List<Student> students = session.selectList("byName", name); SqlSessionUtil.closeSqlSession(); return students; } /** * 根據學生對象模糊查詢學生列表:傳入的參數對象 */ @Override public List<Student> byNameList(Student stu) { List<Student> students = session.selectList("byStu", stu); SqlSessionUtil.closeSqlSession(); return students; } /** * 更新學生信息 */ @Override public int updateStudent(Student stu) { int result = session.update("updatestu", stu); session.commit(); SqlSessionUtil.closeSqlSession(); return result; } }
給Dao配置俗話中講的小配置(StudentDAO.xml)oracle
<?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.dao"> <!-- 添加一條學生記錄 --> <insert id="insertStudent" parameterType="student"> insert into student(id,age,name) values(id.nextval,#{age},#{name}) <!-- 自增列的值 --> <selectKey keyProperty="id" resultType="int"> SELECT ID.Nextval as ID from DUAL </selectKey> </insert> <!-- 根據id刪除學生記錄 --> <delete id="deletStudent"> delete from student where id=#{value} </delete> <!-- 查詢全部學生記錄 --> <select id="studentlist" resultType="student"> select * from student </select> <!-- 更新學生信息 --> <update id="updatestu" parameterType="student"> update student set age=#{age},name=#{name} where id=#{id} </update> <!-- 模糊查詢 當入參是普通字符串時 --> <select id="byName" resultType="student"> select * from student where name like concat('%',#{name},'%') <!-- 方式一 --> <!-- select * from student where name like '%${value}%' --> <!-- select * from student where name like concat('%',#{name},'%') --> </select> <!-- 模糊查詢 當入參是對象時 --> <select id="byStu" resultType="student"> <!-- 方式一 --> <!-- select * from student where name like '%${name}%' --> <!-- 方式二 --> select * from student where name like concat('%',#{name},'%') </select> </mapper>
接下來咱們配置My batis的配置文件(mybatis-config.xml)app
<?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> <!-- 經過package, 能夠直接指定package的名字, mybatis會自動掃描你指定包下面的javabean, 而且默認設置一個別名,默認的名字爲: javabean 的首字母小寫的非限定類名來做爲它的別名 --> <!-- type指的是javabean的徹底限定名 alias就是指代別名 --> <!-- <typeAlias alias="student" type="cn.entity.Student" /> --> <package name="cn.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="test" /> <property name="password" value="test" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/dao/StudentDAO.xml" /> </mappers> </configuration>
咱們把建立Session提到一個工具類中也就是咱們的Util類ide
package cn.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; /** * 這個工具類主要用來建立sqlsession對象 * @author hyj * */ public class SqlSessionUtil { //定義reader對象 static Reader reader; static SqlSessionFactory sqlSessionFactory; static SqlSession sqlsession; //靜態代碼初始化給reader,sessionFactory初始化 static{ try { reader=Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } /** * 獲取SqlSession對象 * @return SqlSession對象 */ public static SqlSession getSqlSession(){ sqlsession=sqlSessionFactory.openSession(); return sqlsession; } /** * 關閉sqlsession */ public static void closeSqlSession(){ if(sqlsession!=null){ sqlsession.close(); } } }
最後咱們使用測試類測試便可工具
package cn.test; import java.io.IOException; import java.util.List; import org.junit.Before; import org.junit.Test; import cn.dao.StudentDao; import cn.dao.impl.StudenDaoImpl; import cn.entity.Student; public class StudentTest { StudentDao sd; @Before public void before() { // 建立Studentdao對象 sd = new StudenDaoImpl(); } /** * 添加學生信息 * * @throws IOException */ @Test public void addTest() throws IOException { // 建立學生對象 Student stu = new Student(); stu.setAge(20); stu.setName("笑話"); System.out.println("保存前:\n"+stu); // 調用dao保存 int save = sd.save(stu); System.out.println("保存後:\n"+stu); System.out.println("保存成功"); } /** * 刪除學生信息 * * @throws IOException */ @Test public void deleteTest() throws IOException { // 調用dao保存 int save = sd.delete(2); System.out.println("刪除成功"); } /** * 查看學生列表 */ @Test public void studentList() { List<Student> studentList = sd.studentList(); for (Student student : studentList) { System.out.println(student); } } /** * 更新學生信息 */ @Test public void updateStudent() { Student stu = new Student(); stu.setAge(99); stu.setName("呵呵"); stu.setId(5); int result = sd.updateStudent(stu); System.out.println("更新成功"+result); } /** * 根據學生姓名模糊查詢學生列表:當傳遞的參數是普通類型時 */ @Test public void byNameList() { List<Student> studentList = sd.byNameList("1"); for (Student student : studentList) { System.out.println(student); } } /** * 根據學生對象模糊查詢學生列表:當傳遞的參數是對象時 */ @Test public void byStuList() { // 建立學生對象 Student stu = new Student(); stu.setName("1"); List<Student> studentList = sd.byNameList(stu); for (Student student : studentList) { System.out.println(student); } } }
這就是咱們的入門案例