MyBatis(二)MyBatis入門程式(MyBatis demo)
建立工程
mybatis-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>
<!-- 定義別名 -->
<typeAliases>
<typeAlias alias="student" type="cn.bing.pojo.Student"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- 採用JDBC的事務管理方式 -->
<transactionManager type="JDBC">
<property name="autoCommit" value="false"/>
</transactionManager>
<!-- 配置資料庫的連結資訊 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 定義對映器,引入的xml檔案的作用是,提供了SQL和SQL對POJO的對映規則定義,
它包含了對映器裡面的資訊,Mybatis將會為我們解析這個xml生成對映器
-->
<mappers>
<mapper resource="cn\bing\mapper\StudentMapper.xml"/>
</mappers>
</configuration>
StudentMapper.xml的路徑是\分隔
獲取SqlSessionFactory
因為只是用來獲取sqlSession,需要多次呼叫,定義為單例的,節省物件的建立資源。
public class SqlFactoryUtil {
private SqlFactoryUtil(){}
private static final Class CLASS_LOCK = SqlFactoryUtil.class;
private static SqlSessionFactory factory = null;
public static SqlSessionFactory getSqlSessionFactory(){
InputStream input = null;
try {
input = Resources.getResourceAsStream("mybatis-config.xml");
synchronized (CLASS_LOCK) {
if(factory==null){
factory = new SqlSessionFactoryBuilder().build(input);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return factory;
}
}
對映的pojo物件
定義為javaBean風格
public class Student {
private Integer stuId;
private Integer stuAge;
private Integer stuSex;
private String stuName;
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public Integer getStuAge() {
return stuAge;
}
public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
}
public Integer getStuSex() {
return stuSex;
}
public void setStuSex(Integer stuSex) {
this.stuSex = stuSex;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
定義StudentMapper.java 和StudentMapper.xml
* 格式上要求檔案同名,放在一個包下面
* SQL語句的查詢欄位的名稱,必須和POJO的欄位名一致,才能對映到物件上
* namespace的值是介面的全路徑名
package cn.bing.mapper;
import cn.bing.pojo.Student;
public interface StudentMapper {
public Student queryStudentInfo(int id);
}
<?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.bing.mapper.StudentMapper">
<select id="queryStudentInfo" resultType="student">
select stu_id as stuId,stu_age as stuAge,stu_sex as stuSex,stu_name as stuName
from student_info where stu_id = #{id}
</select>
</mapper>
定義log4j.properties,輸出日誌資訊
log4j.rootLogger = DEBUG,stdout
log4j.logger.org.mybatis = DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C:%m%n
測試類,呼叫查詢方法
SqlSessionFactory factory = SqlFactoryUtil.getSqlSessionFactory();
SqlSession session = null;
try{
session = factory.openSession();
StudentMapper mapper = (StudentMapper) session.getMapper(StudentMapper.class);
Student st = mapper.queryStudentInfo(1);
System.out.println(st.getStuName());
session.commit();
}catch(Exception e){
e.printStackTrace();
session.rollback();
}finally{
//確保資源被關閉
if(session!=null){
session.close();
}
}
相關文章
- 【Mybatis】Mybatis快速入門MyBatis
- mybatis入門程式MyBatis
- Mybatis入門及第一個Mybatis程式MyBatis
- MyBatis系列(一):MyBatis入門MyBatis
- MyBatis從入門到精通(一):MyBatis入門MyBatis
- Mybatis入門MyBatis
- MyBatis 入門MyBatis
- 2018超直白入門demo:IDEA+Maven+MyBatisIdeaMavenMyBatis
- mybatis快速入門MyBatis
- MyBatis(一) 入門MyBatis
- MyBatis 框架之快速入門程式MyBatis框架
- MyBatis研習錄(01)——MyBatis概述與入門MyBatis
- MyBatis從入門到精通(二):MyBatis XML方式的基本用法之SelectMyBatisXML
- Mybatis批量操作demoMyBatis
- Mybatis 入門介紹MyBatis
- Mybatis極速入門MyBatis
- mybatis入門學習MyBatis
- MyBatis--快速入門MyBatis
- 深入淺出Mybatis原始碼系列(一)---Mybatis入門MyBatis原始碼
- mybatis入門程式:刪除、更新使用者&&hibernate和mybatis的區別MyBatis
- Mybatis框架 入門學習MyBatis框架
- MyBatis入門——瞭解配置MyBatis
- 30分鐘入門MyBatisMyBatis
- 由表生成程式碼:mybatis-generator入門MyBatis
- MyBatis從入門到精通(五):MyBatis 註解方式的基本用法MyBatis
- Mybatis-Plus入門實踐MyBatis
- Java持久層框架Mybatis入門Java框架MyBatis
- MyBatis-Plus筆記(入門)MyBatis筆記
- Mybatis入門 Mybatis存在的意義 解決的問題 基本操作MyBatis
- MyBatis學習(二)MyBatis
- SpringBoot + Mybatis + Redis 整合入門專案Spring BootMyBatisRedis
- SpringBoot2.x入門:使用MyBatisSpring BootMyBatis
- Mybatis註解開發案例(入門)MyBatis
- MyBatis基於Maven入門例項MyBatisMaven
- MyBatis入門——瞭解基本概念MyBatis
- SpringBoot+MySQL+MyBatis的入門教程Spring BootMySqlMyBatis
- MyBatis從入門到精通(十四):在MyBatis中使用型別處理器MyBatis型別
- MyBatis從入門到精通(六):MyBatis動態Sql之if標籤的用法MyBatisSQL