MyBatismyBati初学

创设数据库(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"&gt;

<!– 通过这几个布局文件,达成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:
增删除操作:不能 写
查询:单个实体的档次
–>
MyBatis,<!–查询所有–>
<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带条件查询结果

网站地图xml地图