【Mybatis】- 一般开发规范

Mybatis 一般开发规范(mapper.xml)

  1. namespace命令空间:Mapper接口代理使用
  2. namespce:须与mapper接口名字一致,sql片段完成接口方法映射
  3. mapper接口中的方法、参数列表、返回值须一致

sqlMapConfig.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>
    
<!-- 加载数据库属性文件:db.properties -->
<properties resource="db.properties"/>
 
<!-- typeAliases:别名 -->
<typeAliases>
   <typeAlias type="com.zhiwei.domain.User" alias="user"/>
   <!-- 批量定义别名:mybatis自动扫描polo类,自动创建别名(类型名) -->
   <package name="com.zhiwei.domain" />
</typeAliases>

<!-- 和spring整合environment配置将抛弃 -->
<environments default="development">
<environment id="development">
    
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC"/>

<!-- 数据连接池 :mybatis管理-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>

<!-- 加载映射文件
     resource:每次只能加载一个文件
     class:只能mapper代理对象执行(mapper接口名和mapper.xml文件名一致,并且在同一个目录)
     package:批量加载:name为mapper文件所在的包名
 -->
<mappers>

   <!-- <mapper class="com.zhiwei.mapper.UserMapper"/>-->
   <mapper resource="com/zhiwei/domain/UserMapper.xml"/>
   <package name="com.zhiwei.mapper"/>
</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">

<!--
Mybatis的一般规范:mapper文件
1.namespace命令空间:Mapper接口代理使用
2.namespce:必须和mapper接口名字一致
3.mapper接口中的方法、参数列表、返回值必须一致

映射文件配置sql语句 :select执行数据库查询
id:sql语句封装mappedStatement的id
#{id}:占位符,表示接受输入的参数:id,如果输入参数类型为简单类型,
#{}中的参数名可以任意,
resultType:返回结果类型
 -->
<mapper namespace="com.zhiwei.mapper.UserMapper">
    
<!-- 
resultMap:将对象属性和属性值封装到map,并注入到相应的类
id:表示查询结果集的唯一标识(一般为主键)
result:对普通列的映射
column:查询出来的列
property:type制指定的pojo属性名
 -->
<resultMap type="com.zhiwei.mapper.User" id="userResultMap">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="passwd" property="passwd"/>
</resultMap>

<!-- sql片段自动拼接 -->
<sql id="Condition">
        <if test="id=1">
            id=2;
        </if>
</sql>

<select id="findUser" parameterType="int" resultMap="userResultMap">
    select id,name,passwd from user 
    <!-- 动态sql语句:实现sql语句自动拼接:where id=#{id} -->
    <!-- <where><if test="id=1">id=2;</if></where> -->
    <where>
        <!-- 引用sql语句片段 -->
        <include refid="Condition"/>
    </where>

</select>

<!-- 复杂条件查询:多用户查询:pojo包装类型中的对象属性可以直接引用 -->
<select id="fingUserList" parameterType="com.zhiwei.pojo.UserQueryVo" resultType="com.zhiwei.pojo.UserCustomer">
   <!-- mybatis只会返回的列值,未列出的字段默认值填充,因此id=0 -->
    select name,passwd from user where user.name=#{userCustomer.name} and user.passwd=#{userCustomer.passwd}
</select>

<!-- 如果输入的参数为pojo类型,占位符名称使用pojo的属性 -->   
<select id="findUserById" parameterType="int" resultType="com.zhiwei.mapper.User">
select *from user where id=#{id}
</select>

<delete id="deleteUserById" parameterType="int">
delete from user where id=#{id}
</delete>

<!-- 如果输入的参数为pojo类型,占位符名称使用pojo的属性 -->
<update id="updateUser" parameterType="user">
update  user set name=#{name},passwd=#{passwd} where id=#{id}
</update>

<!-- 包批量定义别名:User为类名(类名首字母大小写都可以) -->
<insert id="addUser" parameterType="User">
insert into user(id,name,passwd) values(#{id},#{name},#{passwd})
</insert>
</mapper>

测试类:

package com.zhiwei.test;

import java.util.List;
import org.apache.ibatis.session.SqlSession;

import com.zhiwei.mapper.User;
import com.zhiwei.mapper.UserMapper;
import com.zhiwei.pojo.UserCustomer;
import com.zhiwei.pojo.UserQueryVo;
import com.zhiwei.tool.MybatisTool;

public class MainTest {

	
	public static void main(String[] args) throws Exception {
		
		SqlSession sqlSession=MybatisTool.getSession();
		//通过Mapper接口,mybatis自动生成代理对象
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		
		UserQueryVo userQueryVo=new UserQueryVo();
		
		UserCustomer userCustomer=new UserCustomer();
		userCustomer.setName("squirrel");
		userCustomer.setPasswd("xiaoyang");
		
		userQueryVo.setUserCustomer(userCustomer);
		
		List<UserCustomer> users=userMapper.fingUserList(userQueryVo);
		System.out.println("pojo:"+users);
		
	    User user=userMapper.findUser(1);
	    System.out.println("resultMap:"+user);
		
	}
}

结果:

DEBUG [main] - Checking to see if class com.zhiwei.mapper.User matches criteria [is assignable to Object]
DEBUG [main] - Checking to see if class com.zhiwei.mapper.UserMapper matches criteria [is assignable to Object]
DEBUG [main] - Openning JDBC Connection
DEBUG [main] - Created connection 1413653265.
DEBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@5442a311]
DEBUG [main] - ==>  Preparing: select name,passwd from user where user.name=? and user.passwd=? 
DEBUG [main] - ==> Parameters: squirrel(String), xiaoyang(String)
pojo:[]
DEBUG [main] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@5442a311]
DEBUG [main] - ==>  Preparing: select id,name,passwd from user WHERE id=2; 
DEBUG [main] - ==> Parameters: 
resultMap:User [id=2, name=lisi, passwd=lisi]

项目结构层次:

转载于:https://my.oschina.net/yangzhiwei256/blog/3017239

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值