mybatis 代理方式对数据库进行操作

时间:2021-10-13 07:58:07

UserMapper.xml文件

<span style="font-family:Courier New;"><?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">
<!-- 这里的namespace命名空间,等于mapper接口的地址 -->
<mapper namespace="cn.itcast.mapper.UserMapper">
	<select id="findById" parameterType="int" resultType="user">
		select * from user where id=#{id}
	</select>
	<select id="findUserByName" parameterType="java.lang.String" resultType="user">
		select * from user where username like "%${value}%"
	</select>
	<update id="updateUser" parameterType="cn.itcast.domain.User">
		update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
	</update>
	<delete id="deleteUser" parameterType="int">
		delete from user where id=#{id}
	</delete>
	<insert id="insertUser" parameterType="cn.itcast.domain.User">
		insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address});
	</insert>
</mapper></span>
举例说明:

<mapper namespace="cn.itcast.mapper.UserMapper">
<select id="findById" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>

namespace:就是UserMapper接口的地址即接口UserMapper的全路径名

id:这里的id的值就是UserMapper接口中定义的方法名,要保持一致

parmeterType:这里的值必须和UserMapper接口中方法的参数名的类型保持一致

resultType:这里的值必须和UserMapper接口中方法的返回值的类型保持一致

Usermapper

package cn.itcast.mapper;

import java.util.List;

import cn.itcast.domain.Customer;
import cn.itcast.domain.User;
import cn.itcast.domain.UserQueryVo;

public interface UserMapper {
	public User findById(int id);
	public List<User> findUserByName(String name);
	public void updateUser(User user);
	public void deleteUser(int id);
	public void insertUser(User user);
	public List<Customer> findAllUsersList(UserQueryVo userQueryVo);
	public  User findUserResultType(int id);
	public int findUserCount();
}

mybatis 代理方式对数据库进行操作