JavaWeb_(Mybatis框架)Mapper动态代理开发_三
系列博文:
JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门
JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二 传送门
JavaWeb_(Mybatis框架)Mapper动态代理开发_三 传送门
JavaWeb_(Mybatis框架)主配置文件介绍_四 传送门
JavaWeb_(Mybatis框架)输入和输出参数_五 传送门
JavaWeb_(Mybatis框架)关联查询_六传送门 传送门
JavaWeb_(Mybatis框架)动态sql_七传送门 传送门
MyBatis Mapper动态代理开发4+1 (4大原则+1注意):
1、接口方法名需要与mapper.xml的要调用的sql语句的id一致;
2、接口的形参类型需要与mapper.xml parameterType一致;
3、接口的返回值需要与mapper.xml resultType一致;
4、mapper.xml中namespace要与接口的全包名一致;
5、注意mapper动态代理开发中,根据返回值类型来自动选择;
在Dao层开发,需要写一个接口,需要写一个实现类,需要一个Mapper.xml三个文件才能操作数据库
Mapper动态代理只需要实现一个接口和一个配置文件,就可以实现以上操作
一、通过 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"> <mapper namespace="com.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User"> select * from user where u_id = #{id} </select> <!-- #{}占位符 尽量使用#{}来解决问题 --> <!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value --> <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User"> <!-- select * from user where u_username like '%${value}%' --> select * from user where u_username like "%"#{name}"%" </select> <!-- 添加用户 参数为全包名 --> <insert id="insertUser" parameterType="com.Gary.bean.User"> insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid}) </insert> <!-- 根据id修改username字段的语句 --> <update id="updateUser" parameterType="com.Gary.bean.User"> update user set u_username = #{u_username} where u_id = #{u_id} </update> <!-- 根据id删除用户 --> <delete id="deleteUserById" parameterType="Integer"> delete from user where u_id = #{id} </delete> </mapper>
package com.Gary.test; 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 org.junit.Test; import com.Gary.bean.User; import com.Gary.mapper.UserMapper; public class MapperTest { @Test public void Test1() throws IOException { //读取配置文件 String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.selectUserById(1); System.out.println(user); } }
二、通过名字模糊查询用户列表
<?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.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="com.Gary.bean.User"> select * from user where u_id = #{id} </select> <!-- #{}占位符 尽量使用#{}来解决问题 --> <!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value --> <select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User"> <!-- select * from user where u_username like '%${value}%' --> select * from user where u_username like "%"#{name}"%" </select> <!-- 添加用户 参数为全包名 --> <insert id="insertUser" parameterType="com.Gary.bean.User"> insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid}) </insert> <!-- 根据id修改username字段的语句 --> <update id="updateUser" parameterType="com.Gary.bean.User"> update user set u_username = #{u_username} where u_id = #{u_id} </update> <!-- 根据id删除用户 --> <delete id="deleteUserById" parameterType="Integer"> delete from user where u_id = #{id} </delete> </mapper>
package com.Gary.test; import java.io.IOException; import java.io.InputStream; import java.util.List; 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 org.junit.Test; import com.Gary.bean.User; import com.Gary.mapper.UserMapper; public class MapperTest2 { @Test public void Test2() throws IOException { //读取配置文件 String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> list = mapper.selectUserByName("王"); for(User u : list) { System.out.println(u); } } }