CREATE TABLE `order_worker` (
`o_id` varchar(255) DEFAULT NULL,
`w_id` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of order_worker
-- ----------------------------
INSERT INTO `order_worker` VALUES ('sylg20180521112014', 'sylg20180509093803');
package com.second.pojo;
public class OrderWorker {
private String orderId;
private String workerId;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getWorkerId() {
return workerId;
}
public void setWorkerId(String workerId) {
this.workerId = workerId;
}
@Override
public String toString() {
return "OrderWorker{" +
"orderId='" + orderId + '\'' +
", workerId='" + workerId + '\'' +
'}';
}
}
package com.second.dao;
import com.second.pojo.OrderWorker;
import java.util.List;
import java.util.Map;
public interface OrderWorkerDao {
int insertSelective(OrderWorker orderWorker);
List<Map<String, Object>> selectByOrderId(String orderId);
}
- 创建一个Mybatis使用的配置文件:SQL映射文件,用于专门写SQL语句,一个表对应一个SQL映射文件,文件类型为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.second.dao.OrderWorkerDao" >
<insert id="insertSelective">
insert into order_worker
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="orderId != null">
o_id,
</if>
<if test="workerId != null">
w_id
</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="orderId != null">
#{orderId},
</if>
<if test="workerId != null">
#{workerId}
</if>
</trim>
</insert>
<select id="selectByOrderId" resultType="map">
select * from v_orderpart where o_id = #{orderId};
</select>
</mapper>
- 创建Mybatis主配置文件(mybatis.xml),一个项目对应一个主配置文件,主配置文件提供了数据库的连接信息和SQL映射文件的位置信息
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="com.second.pojo"/>
</typeAliases>
<mappers>
<package name="com.second.dao"/>
</mappers>
</configuration>
- 创建测试类使用Mybatis,通过Mybatis访问数据库
@Test
public void selectByOrderId1 () throws Throwable
{
//1.定义Mybatis主配置文件的名称,从类路径的根(target/classes)开始
String config = "mybatis.xml";
//2.读取config表示的文件
InputStream resourceAsStream = Resources.getResourceAsStream(config);
//3.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//4.创建SqlSessionFactory对象
SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
//5.获取SqlSession对象,从SqlSessionFactory中获取SqlSession【重要】
SqlSession sqlSession = build.openSession();
//6.通过动态代理获取要执行的mapper
OrderWorkerDao mapper = sqlSession.getMapper(OrderWorkerDao.class);
//7.通过mapper调用要执行的方法从而执行响应的SQL语句
List<Map<String, Object>> list = mapper.selectByOrderId("");
for (Map<String, Object> map : list) {
System.out.println(map);
}
if (sqlSession != null)
{
sqlSession.close();
}
}