Mybatis學習筆記之一——牛刀小試
阿新 • • 發佈:2017-05-09
ans ive man 配置 typealias 操作 acc esp 配置文件
1、Mybaits核心對象SqlSession的作用:
(1)向SQL語句傳入參數;
(2)執行SQl語句;
(3)獲取執行SQL語句的結果;
(4)事務的控制;
2、核心配置文件(Configration.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> <!-- <settings> <setting name="useGeneratedKeys" value="false"/> <setting name="useColumnLabel" value="true"/> </settings> <typeAliases> <typeAlias alias="UserAlias" type="org.apache.ibatis.submitted.complex_property.User"/> </typeAliases> --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/><!-- 配置sql驅動 --> <property name="url" value="jdbc:mysql://localhost:3306/school"/><!-- 配置數據庫地,其中school為數據名稱 --> <!-- jdbc:mysql://127.0.0.1:3306/school --> <property name="username" value="root"/><!-- 配置數據庫用戶名 --> <property name="password" value="********"/><!-- 配置數據庫密碼--> </dataSource> </environment> </environments> <mappers> <mapper resource="com/sysker/config/Students.xml"/><!-- 需要導入的xml配置文件,一般用於配置表格數據表格 --> </mappers> </configuration>
3、獲取SqlSession:
(1)通過配置文件獲取數據庫連接相關信息:
Reader reader = Resources.getResourceAsReader("com/sysker/config/Configuration.xml");
(2)通過配置信息構建SqlSessionFactory:
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
(3)通過sqlSessionFactory打開數據庫會話:
session = sqlSessionFactory.openSession();
下面是完整代碼:
package com.sysker.db; 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; /** * 訪問數據庫類 * */ public class DBAccess { public SqlSession getSqlSession() throws IOException{ SqlSession session =null; //通過配置文件獲取數據庫連接信息 Reader reader = Resources.getResourceAsReader("com/sysker/config/Configuration.xml"); //通過配置信息構建一個SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //通過sqlsessionFactory打開一個數據庫會話 session = sqlSessionFactory.openSession(); return session; } }
4、配置與數據表格相關的sql語句(Students.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"> <mapper namespace="Studnets"> <resultMap type="com.sysker.beans.Students" id="StudentsResult"> <id column="id" jdbcType="INTEGER" property="id"/> <result column="name" jdbcType="VARCHAR" property="name"/> <result column="classname" jdbcType="VARCHAR" property="classname"/> <result column="sex" jdbcType="VARCHAR" property="sex"/> <result column="age" jdbcType="VARCHAR" property="age"/> </resultMap> <select id="getAllStudents" resultMap="StudentsResult"> SELECT id,name,classname,sex,age FROM student </select>
</mapper>
5、通過Dao層來執行SQL操作:
package com.sysker.dao; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.sysker.beans.Students; import com.sysker.db.DBAccess; import com.sysker.jdbc.DBContol; public class StudentDAO { public List<Students> getAllStudents(){ DBAccess dbAccess = new DBAccess(); List<Students> studentsList = new ArrayList<Students>(); SqlSession sqlSession = null; try { sqlSession = dbAccess.getSqlSession(); //通過sqlSession執行sql語句 studentsList = sqlSession.selectList("Students.getAllStudents"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(sqlSession!=null){ sqlSession.close(); } } return studentsList; } public static void main(String[] args) { StudentDAO studentDAO = new StudentDAO(); List<Students> studentsList = new ArrayList<Students>(); studentsList = studentDAO.getAllStudents(); for (Students students : studentsList) { System.out.println(students.toString()); } } }
Mybatis學習筆記之一——牛刀小試