1、前置环境
MyBatis快速入门——第一章、idea的maven配置与demo实例
2、创建【com.item.mapper.模型Mapper.java】文件
创建位置
编码:
@Param("传递参数标识符")
package com.item.mapper; import com.item.model.Users; import org.apache.ibatis.annotations.Param; public interface UsersMapper { /** * 根据id查询 * @param id * @return */ Users SelectById(@Param("id")int id); /** * 根据id查询 * @param nickName * @return */ Users SelectByName(@Param("nickName")String nickName); }
3、修改【模型Mapper.xml】配置文件
#和$两者含义不同
【#】会把传入的数据都当成一个字符串来处理,会在传入的数据上面加一个双引号来处理。
【$】则是把传入的数据直接显示在sql语句中,不会添加双引号。
<?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.item.mapper.UsersMapper"> <select id="GetInfo" resultType="Users"> select * from users </select> <select id="SelectById" resultType="Users" parameterType="java.lang.Integer"> select * from users where id=#{id} </select> <select id="SelectByName" resultType="Users" parameterType="java.lang.String"> select * from users where nickName like "%${nickName}%" </select> </mapper>
4、测试
测试编码:
package com.item.action; import com.item.mapper.UsersMapper; import com.item.model.Users; 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 java.io.IOException; import java.io.Reader; public class Action { public static void main(String[] args) throws IOException { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession = sessionFactory.openSession(); UsersMapper db = sqlSession.getMapper(UsersMapper.class); Users users = db.SelectById(2); Sout(users); Users users1 = db.SelectByName("龙"); Sout(users1); sqlSession.close(); } /** * 输出用 * @param users */ public static void Sout(Users users){ System.out.println("编号:"+users.getId()); System.out.println("创建时间:"+users.getCreateDate()); System.out.println("昵称:"+users.getNickName()); System.out.println("性别:"+(users.getSex()==0?"女":"男")); System.out.println("年龄:"+users.getAge()); System.out.println("简介:"+users.getIntroduce()); } }
测试成功:
5、多表联合查询测试
在【模型Mapper.xml】配置文件中添加
<select id="GetUserInfo" resultType="UserInfo"> select users.id, users.createDate, users.nickName, users.sex, users.age, users.introduce, userInfo.uid, userInfo.phone from users inner join userInfo on users.id=userInfo.uid </select>
由于没有【UserInfo】类,需要在【model】下创建,为了测试方便,我只添加了【phone】列
package com.item.model; public class UserInfo extends Users{ private int uid; private String phone; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
在【com.item.mapper.UserMapper.java】文件中添加【查询所有的方法】
package com.item.mapper; import com.item.model.UserInfo; import com.item.model.Users; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UsersMapper { /** * 查询所有 * @return */ List<UserInfo> GetUserInfo(); /** * 根据id查询 * @param id * @return */ Users SelectById(@Param("id")int id); /** * 根据id查询 * @param nickName * @return */ Users SelectByName(@Param("nickName")String nickName); }
由于没有参数,直接遍历就行:
package com.item.action; import com.item.mapper.UsersMapper; import com.item.model.UserInfo; import com.item.model.Users; 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 java.io.IOException; import java.io.Reader; import java.util.List; public class Action { public static void main(String[] args) throws IOException { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession = sessionFactory.openSession(); UsersMapper db = sqlSession.getMapper(UsersMapper.class); List<UserInfo> userInfos = db.GetUserInfo(); for (UserInfo u:userInfos) { Sout(u); } } /** * 输出用 * @param users */ public static void Sout(UserInfo users){ System.out.println("编号:"+users.getId()); System.out.println("创建时间:"+users.getCreateDate()); System.out.println("昵称:"+users.getNickName()); System.out.println("性别:"+(users.getSex()==0?"女":"男")); System.out.println("年龄:"+users.getAge()); System.out.println("简介:"+users.getIntroduce()); System.out.println("手机:"+users.getPhone()); } }