Mybatis --- 映射文件、参数处理、参数值的获取、select元素
public interface EmployeeMapper {
/*
* 增删改查方法
* */
public Employee getEmployeeById(Integer id);
public void insertEmp(Employee employee);
}
<select id="getEmployeeById" resultType="employee" databaseId="mysql">
select * from student where id = #{id}
</select> <insert id="insertEmp" parameterType="com.neuedu.entity.Employee" useGeneratedKeys="true" keyProperty="id">
insert into student(name,password,email) values(#{name},#{password},#{email})
</insert>
编写测试单元:
private EmployeeMapper mapper = null;
private SqlSession session = null;
@Before
public void testBefore(){
//1.获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//2.利用sqlSessionFactory创建一个session对象,表示和数据库的一次会话
session = sqlSessionFactory.openSession();
//3.用session对象获取mapper接口的代理对象
//因为sql映射文件给相应的接口创建了一个代理对象,所以mapper接口类不需要实现类
mapper = session.getMapper(EmployeeMapper.class);
} @Test
public void testSelect(){
mapper = session.getMapper(EmployeeMapper.class);
//4.通过mapper接口的代理对象就可以对数据库进行增删改查操作
Employee employee = mapper.getEmployeeById(4);
System.out.println(employee);
}
@Test
public void testInsert(){
Employee emp = new Employee("zhangsan", "1234567", "zhangsan@q.com");
mapper.insertEmp(emp);
int id = emp.getId();
System.out.println(id);
}
@After
public void testAfter(){
//增删改需要提交事务
session.commit();
session.close();
}
//@Before、@After自动在@Test之前和之后运行
//查询不需要提交事务,增删改都需要提交事务
2.获取自增主键值【当向数据库中插入一条数据的时候,默认是拿不到主键的值的, 需要设置如下两个属性才可以拿到主键值!】
<!--设置userGeneratedKeys属性值为true:使用自动增长的主键。使用keyProperty设置把主键值设置给哪一个属性-->
<insert id="addEmp" parameterType="com.neuedu.mybatis.bean.Employee" useGeneratedKeys="true" keyProperty="id" databaseId="mysql">
insert into tbl_employee(last_name,email,gender) values(#{lastName},#{gender},#{email})
</insert>
<sql id="npe">
name,password,email
</sql>
<insert id="insertEmp" parameterType="com.neuedu.entity.Employee" useGeneratedKeys="true" keyProperty="id">
insert into student(<include refid="npe"></include>) values(#{name},#{password},#{email})
</insert>
public void updateEmp(@Param("id")Integer id,
@Param("name")String name,
@Param("password")String password,
@Param("email")String email);
<update id="updateEmp">
update student set name=#{name},password=#{password},email=#{email} where id=#{id}
</update>
- POJO参数:如果多个参数正好是我们业务逻辑的数据模型,我们就可以直接传入POJO
#{属性名}:取出传入的POJO的属性值
public void insertEmp(Employee employee);
<sql id="npe">
name,password,email
</sql>
<insert id="insertEmp" parameterType="com.neuedu.entity.Employee" useGeneratedKeys="true" keyProperty="id">
insert into student(<include refid="npe"></include>) values(#{name},#{password},#{email})
</insert>
@Test
public void testReturnVal(){
Employee employee = mapper.getEmployeeById(30);
System.out.println(employee);
}
- Map:如果多个参数不是业务模型中的数据,没有对应的pojo,不经常使用,为了方便,我们也可以传入Map
#{key}:根据 key 取出map中对应的值
public void updateName(Map<String, Object> map);
<update id="updateName">
update student set name=#{name} where id=#{id}
</update>
@Test
public void testMap(){
Map<String, Object> map = new HashMap<>();
map.put("id", 33);
map.put("name", "刘德华");
mapper.updateName(map);
}
${}: 可以获取map中的值获取pojo对象属性的值
preparing:select * from tbl_employee where id = 2 and last_name = ?
<update id="updateEmp">
update student set name=#{name},password=#{password},email=#{email} where id=#{id}
</update>

${}:取出的值直接拼装在sql语句中,会有安全问题
<update id="updateEmp">
update student set name='${name}',password='${password}',email='${email}' where id='${id}'
</update>

public List<Employee> getEmps();
<select id="getEmps" resultType="com.neuedu.entity.Employee">
select * from student
</select>
@Test
public void testReturnList(){
List<Employee> emps = mapper.getEmps();
for (Employee employee : emps) {
System.out.println(employee);
}
}
2.返回记录为一个Map
只能查询单条数据,如果多条的话,多个key 值,找不到
public Map<String, Object> getEmpInfoById(Integer id);
resultType 是 Map 的全类名
<select id="getEmpInfoById" ="java.util.Map">
select * from student where id = #{id}
</select>
key:列名;value:值
@Test
public void testReturnMap(){
Map<String, Object> emp = mapper.getEmpInfoById(30);
Set<Entry<String,Object>> entrySet = emp.entrySet(); for (Entry<String, Object> entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
<settings>
<!-- setting标签负责每一个属性的设置 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
3.利用ResultMap:
public Employee getEmpInfoById(Integer id);
<resultMap type="com.neuedu.entity.Employee" id="getEmployByIdMap">
<!-- 主键映射可以用 id 字段,mybatis在底层会做优化,主键有索引,加快查询速度 -->
<id column="id" property="id"/>
<!-- 普通列的映射使用result -->
<result column="name" property="name"/>
<result column="password" property="password"/>//相同的也可以不写,但因为规范建议写
<result column="email" property="email"/>
</resultMap>
<select id="getEmpInfoById" resultMap="getEmployByIdMap">
select * from student where id = #{id}
</select>
@Test
public void testReturnMap(){
Employee emp = mapper.getEmpInfoById(30);
System.out.println(emp);
}
Mybatis --- 映射文件、参数处理、参数值的获取、select元素的更多相关文章
- mybatis映射文件参数处理 #{}取值与${}取值的区别
#{}:是以预编译的映射,将参数设置到sql语句中,和jdbc的preraredStatement一样,使用占位符,防止sql注入. ${}:取出的值会直接拼装在sql中,会有安全问题. 大多数情况下 ...
- MyBatis映射文件中用#和$传递参数的特点
在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...
- Mybatis映射文件中#取值时指定参数相关规则
Mybatis映射文件中#取值时指定参数相关规则 在#{}中,除了需要的数值外,还可以规定参数的一些其他规则. 例如:javaType,jdbcType,mode(存储过程),numericScale ...
- mybatis映射文件#与$的使用,及参数传入规则
mybaits映射文件中使用#与$场景: <select id="getProviders" resultType="com.lazy.bill.pojo.Prov ...
- Mybatis映射文件的自动映射与手动映射问题
Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...
- MyBatis 映射文件详解
1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...
- MyBatis映射文件 相关操作
一.MyBatis映射文件 1.简介 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行 ...
- MyBatis 映射文件详解(六)
MyBatis 配置文件类型 MyBatis配置文件有两种类型,如下: 全局配置文件(如 mybatis-config.xml) Mapper XML 映射文件(如 UserMapper.xml) 上 ...
- MyBatis 映射文件
Mybatis映射文件简介 1) MyBatis 的真正强大在于它的映射语句.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉 ...
随机推荐
- org.w3c.dom.Element 缺少 setTextContent 步骤
org.w3c.dom.Element 缺少 setTextContent 方法 今天将项目环境由jdk5改为jdk6,eclipse重新编译工程后,却突然出现org.w3c.dom.Element没 ...
- 现代C++新四大名著及C++学习杂谈
现代C++新四大名著及C++学习杂谈 翻开自己的博客,在2012年8月我曾经写过如下一篇博客, <<C++学习的方法以及四大名著>> http://www.cnblogs.co ...
- Ubuntu中使用iptables
(一) 设置开机启动iptables # sysv-rc-conf --level 2345 iptables on (二) iptables的基本命令 1. 列出当前iptables的策略和规则 # ...
- [bzoj1592] Making the Grade
[bzoj1592] Making the Grade 题目 FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能 ...
- 删除物品[JLOI2013]
题目描述 箱子再分配问题需要解决如下问题: (1)一共有N个物品,堆成M堆. (2)所有物品都是一样的,但是它们有不同的优先级. (3)你只能够移动某堆中位于顶端的物品. (4)你可以把任意一 ...
- mysql控制台出现“unknown column 'password' in 'field list'问题
今天在windows系统上使用MySQL命令时,出现下面的"unknown column 'password' in 'field list'问题 解决办法如下,使用authenticati ...
- CSS样式----浮动(图文详解)
标准文档流 宏观地讲,我们的web页面和photoshop等设计软件有本质的区别:web页面的制作,是个"流",必须从上而下,像"织毛衣".而设计软件,想往哪里 ...
- vue指令v-else示例解析
为 v-if 或者 v-else-if 添加 "else 块". <div id="app"> <p v-if="isRender& ...
- 使用 Git 同步时出现ssl错误
错误提示 fatal: unable to access 'https://android.googlesource.com/platform/prebuilts/qemu-kernel/': gnu ...
- 【模板】51Nod--1085 01背包
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为整数),与之相对应的价值为P1,P2--Pn(Pi为整数).求背包能够容纳的最大价值. Input 第1行,2个整数 ...