1.jdbc缺點(diǎn)
jdbc缺點(diǎn)
2.mybatis介紹
mybatis簡(jiǎn)介
mybatis結(jié)構(gòu)
3.配置
3.1依賴
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
3.2mybatis-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">
<!-- 根標(biāo)簽 -->
<configuration>
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis-110?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>
<!-- 環(huán)境坷衍,可以配置多個(gè),default:指定采用哪個(gè)環(huán)境 -->
<environments default="test">
<!-- id:唯一標(biāo)識(shí) -->
<environment id="test">
<!-- 事務(wù)管理器蛉威,JDBC類型的事務(wù)管理器 -->
<transactionManager type="JDBC" />
<!-- 數(shù)據(jù)源脱茉,池類型的數(shù)據(jù)源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis-110" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
<environment id="development">
<!-- 事務(wù)管理器,JDBC類型的事務(wù)管理器 -->
<transactionManager type="JDBC" />
<!-- 數(shù)據(jù)源坠非,池類型的數(shù)據(jù)源 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" /> <!-- 配置了properties浮创,所以可以直接引用 -->
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
</configuration>
3.3 map.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:根標(biāo)簽集侯,namespace:命名空間,隨便寫试躏,一般保證命名空間唯一 -->
<mapper namespace="MyMapper">
<!-- statement猪勇,內(nèi)容:sql語(yǔ)句。id:唯一標(biāo)識(shí)颠蕴,隨便寫泣刹,在同一個(gè)命名空間下保持唯一
resultType:sql語(yǔ)句查詢結(jié)果集的封裝類型,tb_user即為數(shù)據(jù)庫(kù)中的表
-->
<select id="selectUser" resultType="com.zpc.mybatis.User">
select * from tb_user where id = #{id}
</select>
</mapper>
3.3 修改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">
<!-- 根標(biāo)簽 -->
<configuration>
<!-- 環(huán)境,可以配置多個(gè)犀被,default:指定采用哪個(gè)環(huán)境 -->
<environments default="test">
<!-- id:唯一標(biāo)識(shí) -->
<environment id="test">
<!-- 事務(wù)管理器椅您,JDBC類型的事務(wù)管理器 -->
<transactionManager type="JDBC" />
<!-- 數(shù)據(jù)源,池類型的數(shù)據(jù)源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssmdemo" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/MyMapper.xml" />
</mappers>
</configuration>
3.4 實(shí)體類
import java.text.SimpleDateFormat;
import java.util.Date;
public class User {
private String id;
private String userName;
private String password;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private String created;
private String updated;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getUpdated() {
return updated;
}
public void setUpdated(String updated) {
this.updated = updated;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", birthday='" + new SimpleDateFormat("yyyy-MM-dd").format(birthday) + '\'' +
", created='" + created + '\'' +
", updated='" + updated + '\'' +
'}';
}
}
3.5 test類
import com.zpc.test.pojo.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 java.io.InputStream;
public class MybatisTest {
public static void main(String[] args) throws Exception {
// 指定全局配置文件
String resource = "mybatis-config.xml";
// 讀取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 構(gòu)建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 獲取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
// 操作CRUD寡键,第一個(gè)參數(shù):指定statement掀泳,規(guī)則:命名空間+“.”+statementId
// 第二個(gè)參數(shù):指定傳入sql的參數(shù):這里是用戶id
User user = sqlSession.selectOne("MyMapper.selectUser", 1);
System.out.println(user);
} finally {
sqlSession.close();
}
}
}
3.6結(jié)構(gòu)
文件結(jié)構(gòu)
4.mybatis的使用步驟總結(jié)
1)配置mybatis-config.xml 全局的配置文件 (1、數(shù)據(jù)源西轩,2开伏、外部的mapper)
2)創(chuàng)建SqlSessionFactory
3)通過SqlSessionFactory創(chuàng)建SqlSession對(duì)象
4)通過SqlSession操作數(shù)據(jù)庫(kù) CRUD
5)調(diào)用session.commit()提交事務(wù)
6)調(diào)用session.close()關(guān)閉會(huì)話
5. 配置文件詳解
5.1 mybatis-config
jdbc原生管理
<transactionManager type="JDBC" />
轉(zhuǎn)交給其他管理,如spring
<transactionManager type="MANAGED" />
使用數(shù)據(jù)庫(kù)連接池
<dataSource type="POOLED">
不使用數(shù)據(jù)庫(kù)連接池
<dataSource type="UNPOOLED">
其他語(yǔ)言連接
<dataSource type="JNDI">
5.2數(shù)據(jù)庫(kù)連接池
在內(nèi)存中開辟一塊空間遭商,存放多個(gè)數(shù)據(jù)庫(kù)連接對(duì)象固灵。
JDBC Tomcat Pool:直接由JDBC產(chǎn)生連接池
狀態(tài):
active:當(dāng)前連接對(duì)象被應(yīng)用程序使用中
idle:空閑狀態(tài),等待應(yīng)用程序使用
目的:
在高頻率訪問數(shù)據(jù)庫(kù)時(shí)劫流,使用數(shù)據(jù)庫(kù)連接池可以降低服務(wù)器系統(tǒng)壓力巫玻,提升服務(wù)器運(yùn)行效率,小型項(xiàng)目不適用數(shù)據(jù)庫(kù)連接池祠汇。
實(shí)現(xiàn)JDBC tomcat pool
在web項(xiàng)目的META.INF中存放context.xml,tomcat中所有項(xiàng)目所共享仍秤。
<Resource
driverClassName=""
url=""
usename=""
password=""
maxActive=""
maxIdle=""
name="test"
auth="Container"
maxWait="10000"
type="javax.sql.Datasource"
/>