MyBatis映射文件--增删改查
数据库表
EmployeeMapper.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.cn.mybatis.dao.EmployeeMapper">
<!--
namespace: 名称空间 id:唯一标识 resultType: 返回值类型 #{id} 从传递过来的参数中取出id
-->
<select id="getEmpById" resultType="com.cn.zhu.bean.Employee">
select * from tbl_employee
where id = #{id}
</select>
<insert id="addEmp" parameterType="com.cn.zhu.bean.Employee">
insert into tbl_employee(last_name,gender,email) values(#{lastName},#{gender},#{email})
</insert>
<update id="updateEmp">
update tbl_employee set last_name=#{lastName},gender=#{gender},email=#{email}
where id=#{id}
</update>
<delete id="deleteEmp">
delete from tbl_employee where id=#{id}
</delete>
</mapper>
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>
<!--
1. mybatis可以使用properties来引入外部properties配置文件的内容
resource: 引入类路径下的资源
url: 引入网路路径或者磁盘路径下的资源
-->
<properties resource="dbconfig.properties"></properties>
<!--
2 settings包含很多设置项
setting:用来设置每一个设置项
name 设置项名
value 设置项取值
-->
<settings >
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--
3 typeAliases: 别名处理器,可以为我们的java类型起个别名
别名不区分大小写
-->
<typeAliases>
<!-- typeAlias : 为某个java类型起个别名
type: 指定要起别名的类型全类名 默认别名就是类名小写,employee
alias 指定新的类名
--><!--
<typeAlias type="com.cn.zhu.bean.Employee" alias="emp"/>
-->
<!-- package: 为 某个包下的所有类批量起别名
name: 指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认名(类名小写))
-->
<package name="com.cn.zhu.bean"/>
<!--3 批量起别名情况下,使用@Alias注解为某个类型指定新的别名 -->
</typeAliases>
<!-- 4 environments: 环境们 ,mybatis可以配置多种环境,default指定使用某种环境
environment 配置一个具体的环境信息,必须有两个标签,id 当前环境唯一标识
transactionManager 事务管理器
type 事务管理器的类型 jdbc(jdbcTransactionFactory)|MANAGED(manged)
自定义事务管理器 ,实现TransactionFactory接口,type指定全类名
dataSource:数据源
type:
-->
<!--<environments default="test">
<environment id="test">
<transactionManager type="JDBC"></transactionManager>
<dataSource type=""></dataSource>
</environment>
</environments>
--><environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<!-- 5 databaseIdProvider 支持多数据库厂商
type="Db_venDor"
作用就是得到数据库厂商的标识 mybatis就能根据数据库厂商
MYsql oracle sql sever
--><!--
<databaseIdProvider type="DB_VENDOE">
为不同的数据库厂商起别名字
<property name="MySql" value="mysql"/>
<property name="Oracle" value="oracle"/>
<property name="SQL Server" value="sqlserver"/>
</databaseIdProvider>
--><!--将我们写好的sql映射文件一定要注册到全局配置文件中
mapper: 注册一个sql文件
注册配置文件
resource:引用类路径下的sql映射文件
注册接口
class: 引用(注册)接口
1. 有sql映射文件,映射文件名必须和接口同名,并且放在与接口同一目录下
2 没有sql映射文件 ,所有的sql文件是利用注解方式
-->
<mappers>
<mapper resource="mybatis/mapper/EmployeeMapper.xml" />
</mappers>
<!--<mappers>
<mapper class="com.cn.mybatis.dao.CopyOfEmployeeMapperAnnotation"/>
</mappers>
--></configuration>
dbconfig.properties 数据库资源文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
Employee.java
package com.cn.zhu.bean;
import org.apache.ibatis.type.Alias;
@Alias("emp")
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(Integer id, String lastName, String email, String gender) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [email=" + email + ", gender=" + gender + ", id=" + id
+ ", lastName=" + lastName + "]";
}
}
EmployeeMapper.java
package com.cn.mybatis.dao;
import com.cn.zhu.bean.Employee;
public interface EmployeeMapper {
public Employee getEmpById(int id);
public long addEmp(Employee employee);
public Boolean updateEmp(Employee employee);
public void deleteEmp(int id);
}
测试类MyBatisTest.java
package com.cn.zhu.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
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.Test;
import com.cn.mybatis.dao.CopyOfEmployeeMapperAnnotation;
import com.cn.mybatis.dao.EmployeeMapper;
import com.cn.zhu.bean.Employee;
/**
* 1 接口式编程
* 原生 dao ====> DaoImpI
* mybatis Mapper====>xxMapper.xml
*
* 2 Sql Session 代表 和数据库的一次回话,用完必须关闭
* 3 SqlSession 和 connection 一样她都是非线程安全。
* @author Administrator
*
*/
public class MyBatisTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 1. 根据xml配置文件( 全局配置文件)创建一个sqlsessionFactory对象
* 有数据源一些运行环境信息
* 2 sql映射文件: 配置了每一个sql,以及sql的 封装规则等
* 3 将sql映射文件注册在全局配置文件中
* 4 写代码
* 1 根据全局配置文件得到sqlSessionFactory
* 2 使用sqlsession工厂,获取到sqlsession对象使用池来执行增删改查
* 一个sqlsession就是代表和数据库的一次回话。用完也要关闭
* @throws IOException
*/
/* @Test
public void test() throws IOException{
*//**
* 2 获取sqlsession实例,能直接执行已经映射的sql语句
*//*
SqlSession openSession=getSqlSessionFactory().openSession();
try{
Employee employee= openSession.selectOne("com.cn.mybatis.EmployeeMapper.selectEmp", 1);
System.out.println(employee);
}finally{
openSession.close();
}
}*/
//@Test
/* public void test01() throws IOException{
//1 获取sqlsessionFactory对象
SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
//2 获取sqlsession对象
SqlSession openSession=sqlSessionFactory.openSession();
try{
//3 获取接口实现的类
//会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
EmployeeMapper mapper= openSession.getMapper(EmployeeMapper.class);
Employee employee= mapper.getEmpById(1);
System.out.println(employee);
}finally{
openSession.close();
}
}*/
/* @Test
public void test2() throws IOException{
SqlSessionFactory sqlsessionFactory=getSqlSessionFactory();
SqlSession optinSession=sqlsessionFactory.openSession();
try{
CopyOfEmployeeMapperAnnotation mapper= optinSession.getMapper(CopyOfEmployeeMapperAnnotation.class);
Employee empById=mapper.getEmpById(1);
System.out.println(empById);
}finally{
optinSession.close();
}
}*/
/**
* 测试增删改查
* 1. mybatis允许增删改查直接定义一下 返回值
* interger long boolean
*
*/
@Test
public void test03() throws IOException{
SqlSessionFactory sqlsessionFactory=getSqlSessionFactory();
// 1 获取到的sqlsession不会自动提交数据
SqlSession openSession=sqlsessionFactory.openSession();
try{
//EmployeeMapper mapper=openSession.getMapper(EmployeeMapper.class);
//测试添加
/*EmployeeMapper mapper=openSession.getMapper(EmployeeMapper.class);
Employee employee= new Employee(null, "zhupeng", "zhu@qq.com", "2");*/
//测试修改
EmployeeMapper mapper=openSession.getMapper(EmployeeMapper.class);
Employee employee= new Employee(1, "zhu", "zhu@qq.com", "2");
boolean updateEmp= mapper.updateEmp(employee);
System.out.println(updateEmp);
//测试删除
//mapper.deleteEmp(2);
//手动提交
openSession.commit();
}finally{
}
}