mybatis是一个半自动的轻量级框架,优点是可以将sql交给开发人员编写,具有灵活性
配置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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<?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.uban.mybatis.dao.EmployeeMapper">
<!--
namespace:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
-->
<select id="getEmpById" resultType="com.uban.mybatis.bean.Employee">
select*from employee where id = #{id}
</select>
</mapper>
<property name="username" value="root" /><property name="password" value="1234" /></dataSource></environment></environments><!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 --><mappers><mapper resource="EmployeeMapper.xml" /> </mappers></configuration>
配置mapper.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.uban.mybatis.dao.EmployeeMapper">
<!--
namespace:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
-->
<select id="getEmpById" resultType="com.uban.mybatis.bean.Employee">
select*from employee where id = #{id}
</select>
</mapper>
测试
package com.uban.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 com.uban.mybatis.bean.Employee;
import com.uban.mybatis.dao.EmployeeMapper;
public class Test {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
//直接使用xml配置调用方法
@org.junit.Test
public void test() throws IOException {
SqlSession opensession=getSqlSessionFactory().openSession();
Employee employee=opensession.selectOne("com.uban.mybatis.dao.EmployeeMapper.getEmpById", 1);
System.out.println(employee);
opensession.close();
}
//接口绑定xml,调用接口中的方法
@org.junit.Test
public void test2() throws IOException {
SqlSession opensession=getSqlSessionFactory().openSession();
EmployeeMapper mapper=opensession.getMapper(EmployeeMapper.class);
Employee employee=mapper.getEmpById(1);
System.out.println(employee);
opensession.close();
}
}
接口层使用注解写sql
public interface EmployeeMapperAnnotation {
@Select("select*from employee where id = #{id}")
public Employee getEmpById(Integer id);
}
@org.junit.Test
public void test3() throws IOException {
SqlSession opensession=getSqlSessionFactory().openSession();
EmployeeMapperAnnotation mapper=opensession.getMapper(EmployeeMapperAnnotation.class);
Employee employee=mapper.getEmpById(1);
System.out.println(employee);
opensession.close();
}