Mapper映射器及其接口规则

1、Mapper映射器及其接口规则

(1)根据SQL定义的id属性作为接口的方法名

(2)根据SQL定义的parameterType类型作为方法的参数类型

(3)根据SQL定义的resultTyoe类型,定义方法的返回类型,多行使用List<类型>,单行直接使用类型

(4)将SQL定义文件中的namespace属性指定为包名.接口名
例如:我们有一个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.dao.IUserDao">
    <select id="loginCheck" parameterType="User" resultType="User">
		select * from user where user_name =#{user_name} and user_password=#{user_password}
	</select>
    <select id="checkId" parameterType="User" resultType="User">
		select * from user where user_name =#{user_name}
	</select>
	<insert id="register" parameterType="User"
			useGeneratedKeys="true" keyProperty="id">
        insert into user(user_name,user_password,sex,birthday,phone,address,role_id,create_time)
             values(#{user_name},#{user_password},#{sex},#{birthday},#{phone},#{address},#{role_id},#{create_time})
    </insert>
	<select id="getRoleByUserID" parameterType="User" resultType="Role">

SELECT * FROM user ,role  WHERE role.id=#{user.role_id} AND  role.id=user.role_id and user.user_name=#{user.user_name}
	</select>

	<select id="findAll" parameterType="String" resultType="User">
		SELECT *
    FROM user,role
    WHERE user_name
    LIKE CONCAT('%',#{userName},'%')
	and user.role_id=role.id
	</select>
	<select id="findAllUser"  resultType="User">
			SELECT *
    FROM user,role
    WHERE user.role_id=role.id
	</select>
</mapper>

根据xml文件定义一个接口

package com.dao;

import com.bean.Role;
import com.bean.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface IUserDao {
     User loginCheck(User user);

     int register(User user);

     User checkId(User user);

     Role getRoleByUserID(@Param("user") User user);

     List<User> findAllUser();

     List<User> findAll(String user_name);
}

你可能感兴趣的:(sql,java,ssm)