Mybatis入門示例(一):基礎程式碼
阿新 • • 發佈:2019-01-24
本文為轉載內容
一、開發環境及前期準備:
win7 、JDK1.7、 Intellij Idea、mybatis3.2.8
1、前期資料準備:
新建一個名字叫mybatis的資料庫,建立一個User表,匯入一條資料。
- 建表指令碼:
Create TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(50) DEFAULT NULL,
`userAge` int(11) DEFAULT NULL,
`userAddress` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
- 匯入資料指令碼:
Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong');
二、開發環境搭建:
1、提供mybatis的配置檔案Configuration.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="User" type="com.nextflower.Study.entity.User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED" >
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" />
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappings/models/User.xml"/>
</mappers>
</configuration>
2、建立與資料庫對應的User類:
package com.nextflower.Study.entity;
/**
* Created by root on 2016/3/6.
* @author nextflower
*/
public class User {
private int id;
private String userName;
private String userAge;
private String userAddress;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
}
3、建立這個User類的對映檔案 User.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="com.nextflower.Study.entity.User">
<select id="selectUserByID" parameterType="int" resultType="User">
select * from `user` where id = #{id}
</select>
</mapper>
4、寫一個測試用例測試一下:
package com.nextflower.TestCase.Mybatis;
import com.nextflower.Study.entity.User;
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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.Reader;
/**
* 測試使用mybatis的示例程式碼
* Created by root on 2016/3/6.
* @author nextflower
*/
public class MybatisTest {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
@Before
public void before() {
try{
reader = Resources.getResourceAsReader("Configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void test() {
SqlSession session = sqlSessionFactory.openSession();
try {
User user = session.selectOne("com.nextflower.Study.entity.User.selectUserByID", 1);
System.out.println(user.getUserAddress());
System.out.println(user.getUserName());
} finally {
session.close();
}
}
@After
public void after() {
//System.out.println("do nothing");
}
}
5、執行結果:
6、配置檔案說明:
- Configuration.xml 是 mybatis 用來建立 sessionFactory 用的,裡面主要包含了資料庫連線相關東西,還有 java 類所對應的別名,比如 這個別名非常重要,你在 具體的類的對映中,比如User.xml 中 resultType 就是對應這裡的。要保持一致,當然這裡的 resultType 還有另外單獨的定義方式,後面再說。
- Configuration.xml 裡面 的是包含要對映的類的xml配置檔案。
- 在User.xml 檔案裡面 主要是定義各種SQL 語句,以及這些語句的引數,以及要返回的型別等.