标签:.sql from 输入参数 配置文件 unit 列表 last pat ssi
<?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.lhx.mybatis.mapperproxy.UserMapper"> <!-- 根据id获取用户信息 --> <select id="findUserById" parameterType="int" resultType="com.lhx.mybatis.po.User"> select * from user where id = #{id} </select> <!-- 自定义条件查询用户列表 --> <select id="findUserByUsername" parameterType="java.lang.String" resultType="com.lhx.mybatis.po.User"> select * from user where username like ‘%${value}%‘ </select> <!-- 添加用户 --> <insert id="insertUser" parameterType="com.lhx.mybatis.po.User"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select LAST_INSERT_ID() </selectKey> insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) </insert> <!-- 根据id获取用户信息 --> <select id="findUserByIdAndSex" resultType="com.lhx.mybatis.po.User"> select * from user where id = #{0} and sex = #{1} </select> </mapper>
package com.lhx.mybatis.mapperproxy; import java.util.List; import com.lhx.mybatis.po.User; /** * 用户管理mapper */ public interface UserMapper { // 根据用户id查询用户信息 public User findUserById(int id) throws Exception; // 查询用户列表 public List<User> findUserByUsername(String username) throws Exception; // 添加用户信息 public void insertUser(User user) throws Exception; public User findUserByIdAndSex(int id,int sex) throws Exception; }
<?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> <!-- 和spring整合后 environments配置将废除 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml" /> </mappers> </configuration>
package com.lhx.mybatis.mapperproxy; import java.io.InputStream; import java.util.Date; 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.Before; import org.junit.Test; import com.lhx.mybatis.po.User; import junit.framework.TestCase; public class UserMapperTest extends TestCase { private SqlSessionFactory sqlSessionFactory; protected void setUp() throws Exception { // mybatis配置文件 String resource = "SqlMapConfig-MapperProxy.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); // 使用SqlSessionFactoryBuilder创建sessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } public void testFindUserById() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获取mapper接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); // 调用代理对象方法 User user = userMapper.findUserById(1); System.out.println(user); // 关闭session session.close(); } public void testFindUserByUsername() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findUserByUsername("张"); System.out.println(list.size()); } public void testInsertUser() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获取mapper接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); // 要添加的数据 User user = new User(); user.setUsername("张三"); user.setBirthday(new Date()); user.setSex("1"); user.setAddress("北京市"); // 通过mapper接口添加用户 userMapper.insertUser(user); // 提交 session.commit(); // 关闭session session.close(); } public void testFindUserByIdAndSex() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获取mapper接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); // 调用代理对象方法 User user = userMapper.findUserByIdAndSex(1, 2); System.out.println(user); // 关闭session session.close(); } }
第一种、索引方式
在mapper接口中定义
public User findUserByIdAndSex(int id,int sex) throws Exception;
在mapper.xml中使用
<!-- 多个参数,不写入参类型 --> <select id="findUserByIdAndSex" resultType="com.lhx.mybatis.po.User"> select * from user where id = #{0} and sex = #{1} </select>
第二种、注解参数
在mapper接口中定义
public User findUserByIdAndSex2(@Param("id") int id, @Param("sex") int sex) throws Exception;
在mapper.xml中使用
<select id="findUserByIdAndSex2" resultType="com.lhx.mybatis.po.User"> select * from user where id = #{id} and sex = #{sex} </select>
第三种、map
第四种、po类
java-mybaits-00203-DAO-mapper代理开发方法,多参数【推荐】
标签:.sql from 输入参数 配置文件 unit 列表 last pat ssi
原文地址:http://www.cnblogs.com/bjlhx/p/6819183.html