創建資料庫(y2165) MyBatis環境搭建1.在pom.xml引入依賴2.得替換build節點,為了讓程式編譯在main中所有子包下的配置文件3.構建大配置,位於resources<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configura ...
創建資料庫(y2165)
MyBatis環境搭建
1.在pom.xml引入依賴
2.得替換build節點,為了讓程式編譯在main中所有子包下的配置文件
3.構建大配置,位於resources
<?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">
<!-- 通過這個配置文件,完成mybatis與資料庫的連接 -->
<configuration><!--根節點-->
<!--別名設定-->
<typeAliases>
<package name="cn.happy.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!--
transactionManager:JDBC保證事務的
update
delete
事務分類:JDBC:編程式事務
xxx.beginTransaction()
tx.commit()
tx.rollback()
配置式事務
JDBC|MANAGED
區別
-->
<!-- 配置事物管理採用JDBC -->
<transactionManager type="JDBC"/>
<!--
POOLED:MyBatis內置的連接池
c3p0連接池
POOLED 、UNPOOLED 、JNDI
-->
<!-- POOLED:mybatis的數據源,JNDI:基於tomcat的數據源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///y2165"/>
<property name="username" value="cd"/>
<property name="password" value="000"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/happy/dao/IDeptDAO.xml"/>
</mappers>
</configuration>
4.構建實體類
package cn.happy.entity;
/**
* Created by lenovo on 2017/7/7.
*/
public class Dept {
private Integer deptNo;
private String deptName;
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
5.構建小配置
<?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 ">
<!--namespace:
命名空間:區分不同空間下的同名SQLID
A: findlAll
B: findAll
-->
<mapper namespace="cn.happy.dao.IDeptDAO" >
<!--SQL標簽
id:唯一鎖定到SQL標識
paramenterType:SQL語句的入參 可以省略
resultType:
增刪除操作:不能 寫
查詢:單個實體的類型
-->
<!--查詢所有-->
<select id="getAllList" resultType="Dept">
SELECT * FROM Dept
</select>
<!--待條件查詢-->
<select id="getDeptById" parameterType="int" resultType="Dept">
select * from Dept where deptNo=#{deptNo}
</select>
</mapper>
6.在大配置中關聯小配置
<mappers>
<mapper resource="cn/happy/dao/IDeptDAO.xml"/>
</mappers>
7.書寫測試類
package cn.happy.test;
import cn.happy.dao.IDeptDAO;
import cn.happy.entity.Dept;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Created by lenovo on 2017/7/9.
*/
public class MyBatisTest01 {
//單元測試
@Test
//1.查詢所有使用getMapper()
public void testall() {
//1 獲取到大配置
String path = "mybatis-config.xml";
try {
InputStream is = Resources.getResourceAsStream(path);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
IDeptDAO de = session.getMapper(IDeptDAO.class);
List<Dept> list = de.getAllList();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
//2.待條件查詢 Mapper的使用
public void testGetOneDept() {
String path = "mybatis-config.xml";
try {
InputStream is = Resources.getResourceAsStream(path);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
IDeptDAO dao = session.getMapper(IDeptDAO.class);
Dept dept = dao.getDeptById(1);
System.out.println(dept.getDeptName());
} catch (IOException e) {
e.printStackTrace();
}
}
使用getMapper查詢所有結果
使用getMapper帶條件查詢結果