mybatis 學習--mybatis基本用法入門
mybatis入門
1.建立java web專案
2.新增mybatis的jar包和mysql的驅動jar包
3.在資料庫中建立一個資料庫,並建立一個測試的資料表user並新增資料
4.新增user 的java類
package com.xiaopeng.model;
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;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", userAge=" + userAge + ", userAddress=" + userAddress
+ "]";
}
}
5.新增mybatis的mapper配置檔案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.xiaopeng.dao.UserMapper">
<select id="getUser" resultType="com.xiaopeng.model.User">
select * from user where id = #{id}
</select>
</mapper>
6.新增mybatis的配置檔案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>
<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://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/user.xml"/>
</mappers>
</configuration>
7.新增mybatis的工具類MyBatisUtil 用來建立SqlSessionFactory
package com.xiaopeng.mybatisutil;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* 建立sqlsessionfactor
* @author PengChan
*
*/
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
public static SqlSessionFactory getSqlSessionFactory(){
InputStream inputStream = null;
if(sqlSessionFactory==null){
String resource = "config/mybatis-config.xml";
try {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
return sqlSessionFactory;
} catch (IOException e) {
e.printStackTrace();
}
}
return sqlSessionFactory;
}
}
優化可以只用單利模式來建立sqlsessionfactory
package com.xiaopeng.mybatisutil;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* 建立sqlsessionfactor
* 使用單例模式建立SqlSessionFactory
* @author PengChan
*
*/
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
private static final Class CLASS_LOCK = MyBatisUtil.class;
private MyBatisUtil(){
}
public static SqlSessionFactory getSqlSessionFactory(){
InputStream inputStream = null;
String resource = "config/mybatis-config.xml";
synchronized (CLASS_LOCK) {
if(sqlSessionFactory==null){
try {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(resource));
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sqlSessionFactory;
}
}
8.新增測試類執行測試
package comxiaopeng.test;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.xiaopeng.dao.UserMapper;
import com.xiaopeng.model.User;
import com.xiaopeng.mybatisutil.MyBatisUtil;
public class MyTest {
@Test
public void testDemo01(){
SqlSession sqlSession=null;
try {
sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUser(1L);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}
}
}
9.執行結果
相關文章
- mybatis入門學習MyBatis
- MyBatis從入門到精通(五):MyBatis 註解方式的基本用法MyBatis
- Mybatis框架 入門學習MyBatis框架
- MyBatis入門學習(一)MyBatis
- MyBatis從入門到精通(二):MyBatis XML方式的基本用法之SelectMyBatisXML
- 【mybatis學習之入門(一)】MyBatis
- MyBatis從入門到精通(三):MyBatis XML方式的基本用法之多表查詢MyBatisXML
- MyBatis從入門到精通(四):MyBatis XML方式的基本用法之增刪改MyBatisXML
- MyBatis入門學習-連線oracle實現CURD基本操作MyBatisOracle
- 學習MyBatis必知必會(2)~MyBatis基本介紹和MyBatis基本使用MyBatis
- Mybatis-基本學習(下)MyBatis
- 【Mybatis】Mybatis快速入門MyBatis
- MyBatis研習錄(01)——MyBatis概述與入門MyBatis
- Mybatis學習筆記 2:Mybatis 基本的CURD操作MyBatis筆記
- maven學習(上)- 基本入門用法Maven
- MyBatis(二)MyBatis入門程式(MyBatis demo)MyBatis
- MyBatis入門——瞭解基本概念MyBatis
- MyBatis系列(一):MyBatis入門MyBatis
- MyBatis1:MyBatis入門MyBatis
- MyBatis從入門到精通(一):MyBatis入門MyBatis
- MyBatis 入門MyBatis
- Mybatis入門MyBatis
- MyBatis基礎:MyBatis入門(1)MyBatis
- Mybatis入門 Mybatis存在的意義 解決的問題 基本操作MyBatis
- MyBatis從入門到精通(六):MyBatis動態Sql之if標籤的用法MyBatisSQL
- mybatis入門程式MyBatis
- mybatis快速入門MyBatis
- MyBatis(一) 入門MyBatis
- mybatis入門教程MyBatis
- mybatis學習MyBatis
- Mybatis入門及第一個Mybatis程式MyBatis
- MyBatis從入門到精通(八):MyBatis動態Sql之foreach標籤的用法MyBatisSQL
- Mybatis極速入門MyBatis
- MyBatis--快速入門MyBatis
- 轉載:mybatis入門MyBatis
- Mybatis 入門介紹MyBatis
- MyBatis從入門到精通(七):MyBatis動態Sql之choose,where,set標籤的用法MyBatisSQL
- MyBatis學習(二)MyBatis