<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.37version>
dependency>
主要添加下面这两个jar包:
目前还没有使用spring 进行整合所以这里就使用了这两个jar
package com.mybatis.domian;
public class Employee {
private Integer id;
private String lastName;
private String gender;
private String email;
public Employee() {
super();
}
public Employee(Integer id, String lastName, String gender, String email) {
super();
this.id = id;
this.lastName = lastName;
this.gender = gender;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", gender="
+ gender + ", email=" + email + "]";
}
}
package com.mybatis.mapper;
import java.util.List;
import com.mybatis.domian.Employee;
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public Integer addEmp(Employee emp);
public Integer updateEmp(Employee emp);
public boolean deleteEmp(Integer id);
public List getAll();
}
对于增删改操作,mybatis会自动获取更改的数据条数,也可以是是否值(更改了为true,没有更改为false)
<mapper namespace="com.mybatis.mapper.EmployeeMapper">
<select id="getEmpById" resultType="com.mybatis.domian.Employee">
select * from tbl_employee where id = #{id}
select>
<insert id="addEmp" parameterType="com.mybatis.domian.Employee">
insert into tbl_employee(last_name,gender,email) values(#{lastName},#{gender},#{email})
insert>
<update id="updateEmp" parameterType="com.mybatis.domian.Employee">
update tbl_employee set last_name = #{lastName} where id=#{id}
update>
<delete id="deleteEmp" parameterType="Integer">
delete from tbl_employee where id=#{id}
delete>
<select id="getAll" resultType="com.mybatis.domian.Employee">
select * from tbl_employee
select>
mapper>
- 这个文件的编写是程序员主要的工作,也是mybatis的精髓所在,手动编写sql语句。
- namespace为当前文件的唯一标识,一般指定为一个接口文件如EmployeeMapper .java文件。
- 里面的内容可以看作接口文件每个方法的实现(数据库操作),id对应了方法名。
- select需要指定结果类型,其他的需要指定参数类型。
在类路径下创建mybatis配置文件 mybatis-config.xml :
<configuration>
<properties resource="db.properties">properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${dev_driver}" />
<property name="url" value="${dev_url}" />
<property name="username" value="${dev_username}" />
<property name="password" value="${dev_password}" />
dataSource>
environment>
<environment id="pro">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${prod_driver}" />
<property name="url" value="${prod_url}" />
<property name="username" value="${prod_username}" />
<property name="password" value="${prod_password}" />
dataSource>
environment>
environments>
<mappers>
<mapper class="com.mybatis.mapper.EmployeeMapper"/>
mappers>
configuration>
public class MyBatisUtil {
public static SqlSessionFactory getFactory(){
String resource = "mybatis-config.xml";
//加载 mybatis 的配置文件(它也加载关联的映射文件)
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//构建 sqlSession 的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
return sessionFactory;
}
}
public class EmployeeDao {
public Employee getEmpById(Integer id){
SqlSessionFactory factory = MyBatisUtil.getFactory();
SqlSession session = factory.openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee empById = mapper.getEmpById(id);
return empById;
};
public Integer addEmp(Employee emp){
SqlSessionFactory factory = MyBatisUtil.getFactory();
SqlSession session = factory.openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Integer result = mapper.addEmp(emp);
session.commit();
session.close();
return result;
};
public Integer updateEmp(Employee emp){
SqlSessionFactory factory = MyBatisUtil.getFactory();
SqlSession session = factory.openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Integer result = mapper.updateEmp(emp);
session.commit();
session.close();
return result;
};
public boolean deleteEmp(Integer id){
SqlSessionFactory factory = MyBatisUtil.getFactory();
SqlSession session = factory.openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
boolean result = mapper.deleteEmp(id);
session.commit();
session.close();
return result;
};
public List getAll(){
SqlSessionFactory factory = MyBatisUtil.getFactory();
SqlSession session = factory.openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
List result = mapper.getAll();
session.commit();
session.close();
return result;
};
}
- 这里使用session.getMapper(EmployeeMapper.class) 获取一个代理类,这个代理类是将Mapper xml文件和接口类结合生成的文件。这样我们调用接口方法时节可以指定我们自定义的sql语句了。
- session.commit(),非查询操作才能起作用。如果不想手动提交,可以使用SqlSession session = factory.openSession(true);,这样可以自动提交 session使用完,记得close()
@Insert("insert into tbl_employee(last_name,gender,email) values(#{lastName},#{gender},#{email})")
public Integer insert(Employee emp);
注意:对于Mapper文件别忘记在mybatis-config.xml 文件中注册
<mappers>
<mapper class="com.mybatis.mapper.EmployeeMapper"/>
mappers>
使用myBatis的准备步骤:
1. 配置mybatis-config.xml文件
2. 配置Mapper XML文件(定义接口)
3. 获取SqlSessionFactory
4. 通过SqlSessionFactory得到SqlSession
5. 通过session.getMapper(xx.class) 得到代理对象
6. 调用得到的代理对象的方法操作数据库
7. 提交关闭session