mybatis_2
<?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?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments> <!-- 加载映射文件 -->
<mappers>
<mapper resource="EmployeeMapper1.xml"/>
<mapper resource="EmployeeMapper2.xml"/>
</mappers>
</configuration>
package demo2;
import entity.Employee;
public interface EmployeeMapper {
public Employee getById(int id);
}
<?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"> <!-- demo2 -->
<mapper namespace="com.demo2.EmployeeMapper">
<!--
mybatis 支持命名空间设置为 dao 接口 进行绑定 框架会自动对接口进行代理 不需要手动实例化
sql 唯一id与接口中的方法进行绑定 这里的接口只需要设定好方法的参数而不需要实例化
-->
<select id="getById" resultType="org.entity.Employee">
select * from employee where id = #{id}
</select>
</mapper>
package demo2; 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 entity.Employee; public class Demo2 { public static void main(String[] args) throws IOException {
// 1、获取sessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// 2、获取 session
SqlSession session = sqlSessionFactory.openSession();
// 3、获取接口的是实现类对象 自动代理
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); Employee employee = mapper.getById(1);
System.out.println(employee.toString());
session.close();
}
}
mybatis_2的更多相关文章
- Mybatis_2.基于XML的增删改查
1.实体类User.java public class User { private int id; private String name; private int age; //getter.se ...
随机推荐
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- ES6(Iterator 和 for...of 循环)
Iterator 和 for...of 循环 1.什么是 Iterator 接口 Iterator 接口功能:用一种相同办法的接口让不同的数据结构得到统一的读取命令的方式 2.Iterator的基本用 ...
- NGINX模块(二)
[Nginx标准HTTP模块] 一.HTTP核心模块 指令1:alias 语法:alias file-path|directory-path; 默认值:no 使用字段:location 说明:这个指令 ...
- Attribute "not-null" must be declared for element type "property"解决办法
Attribute "not-null" must be declared for element type "property"解决办法 在hiberante ...
- Codeforces947D. Picking Strings
$n \leq 100000,m \leq 100000$,给长度$n$的字符串$s$和$m$的字符串$t$,只含ABC.定义串$a$可以经过任意次如下操作变成其他串. 现在$q \leq 10000 ...
- uva 662
dp +路径输出 #include <cstdio> #include <cstdlib> #include <cmath> #include <stack& ...
- "undefined reference to strptime"之自己定义strptime函数
简单介绍 strptime()函数可以依照特定时间格式将字符串转换为时间类型.简单点说可以将字符串时间转化为时间戳. 这个函数包括在time.h头文件里,在Unix或者类Unix系统中,我们会常常 ...
- Linux 修改终端命令提示符颜色
相信很多人已经看厌了Linux已成不变的命令提示符的颜色,多数人要么使用默认的绿色,要么在使用PUTTY的时候设置成绿色的,不知道是否有人想到提示符可以设置成其他的颜色呢,本文就说明命令提示符变量PS ...
- Word 2013安裝字典
不必從內建的字典中開始,Word 2013 可將您連結到 Office 市集,方便您挑選免費的字典,或從包括多語字典的字典集合中購買. 若要選擇並安裝您想要的字典,請以滑鼠右鍵按一下任何單字,並按一下 ...
- Null value was assigned to a property of primitive type setter of原因及解决
出现Null value was assigned to a property of primitive type setter of错误是由于类型不匹配,将字段的属性由hibernate的int类型 ...