JavaWeb_(Mybatis框架)输入和输出参数_五
系列博文:
JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门
JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二 传送门
JavaWeb_(Mybatis框架)Mapper动态代理开发_三 传送门
JavaWeb_(Mybatis框架)主配置文件介绍_四 传送门
JavaWeb_(Mybatis框架)输入和输出参数_五 传送门
JavaWeb_(Mybatis框架)关联查询_六传送门 传送门
JavaWeb_(Mybatis框架)动态sql_七传送门 传送门
1、输入映射parameterType;
a)基本类型;
b)自定义对象;
c)自定义包装类;
2、输出映射resultType、resultMap;
a)resultType:
i.基本类型;
ii.自定义对象;
iii.集合;
b)resultMap;
i.bean对象字段与数据表字段不匹配;
ii.自定义包装类;
iii.关联查询;
1、输入映射parameterType[废弃的]
创建一个自定义对象UserVo
package com.Gary.bean; //包装类 public class UserVo { //包装类 //需要全部的user对象信息 private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } //需要表B的id name 属性 }
在UserMapper接口中定义一个selectUserByVoId(UserVo id)方法,通过UserVo id 查询一个用户
package com.Gary.mapper; import java.util.List; import com.Gary.bean.User; import com.Gary.bean.UserVo; public interface UserMapper { //通过id查询一个用户 public User selectUserById(Integer id); //通过用户名模糊查询 获取用户列表 public List<User> selectUserByName(String name); //通过UserVo id 查询一个用户 public User selectUserByVoId(UserVo id); }
在UserMapper.xml中实现这个查询方法
<!-- 根据UserVo中的User对象的u_id去查询查询用户 --> <select id="selectUserByVoId" parameterType="UserVo" resultType="user"> select * from user where u_id = #{user.u_id} </select>
<?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="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> <!-- 根据UserVo中的User对象的u_id去查询查询用户 --> <select id="selectUserByVoId" parameterType="UserVo" resultType="user"> select * from user where u_id = #{user.u_id} </select> </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.bean.UserVo; import com.Gary.mapper.UserMapper; public class MapperTest3 { @Test public void Test3() 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); UserVo vo = new UserVo(); User u= new User(); u.setU_id(5); vo.setUser(u); User user = mapper.selectUserByVoId(vo); System.out.println(user); } }
2、输出映射resultType
实现查询数据库中总条数
在CountryMapper.java中编写接口selectAll(),UserMapper.xml中是实现selectUserCount查询用户总条数sql语句
<!-- 查询用户的总条数 --> <select id="selectUserCount" resultType="Integer"> select count(*) from user </select>
<?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="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> <!-- 根据UserVo中的User对象的u_id去查询查询用户 --> <select id="selectUserByVoId" parameterType="UserVo" resultType="user"> select * from user where u_id = #{user.u_id} </select> <!-- 查询用户的总条数 --> <select id="selectUserCount" resultType="Integer"> select count(*) from user </select> </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.bean.UserVo; import com.Gary.mapper.UserMapper; public class MapperTest4 { @Test public void Test4() 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); Integer count = mapper.selectUserCount(); System.out.println(count); } }
3、输出映射resultMap
创建一个Country.class
package com.Gary.bean; public class Country { private Integer id; private String c_countryname; private String c_capital; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getC_countryname() { return c_countryname; } public void setC_countryname(String c_countryname) { this.c_countryname = c_countryname; } public String getC_capital() { return c_capital; } public void setC_capital(String c_capital) { this.c_capital = c_capital; } @Override public String toString() { return "Country [id=" + id + ", c_countryname=" + c_countryname + ", c_capital=" + c_capital + "]"; } }
在CountryMapper.java接口中定义一个查询所有国家的方法selectAll()
package com.Gary.mapper; import java.util.List; import com.Gary.bean.Country; public interface CountryMapper { //查询所有 List<Country> selectAll(); }
在CountryMapper.xml中编写Sql查询语句,因为数据库中没有id这个属性类名,所以需要一个<resultMap>去将id映射成c_id
<mapper namespace="com.Gary.mapper.CountryMapper"> <resultMap type="Country" id="country"> <result property="id" column="c_id"/> </resultMap> <!-- 查询所有 --> <select id="selectAll" resultMap="country"> select * from country </select> </mapper>
<?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.CountryMapper"> <resultMap type="Country" id="country"> <result property="id" column="c_id"/> </resultMap> <!-- 查询所有 --> <select id="selectAll" resultMap="country"> select * from country </select> </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.Country; import com.Gary.bean.User; import com.Gary.bean.UserVo; import com.Gary.mapper.CountryMapper; import com.Gary.mapper.UserMapper; public class MapperTest5 { @Test public void Test4() throws IOException { //读取配置文件 String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession SqlSession session = ssf.openSession(); CountryMapper mapper = session.getMapper(CountryMapper.class); List<Country> list = mapper.selectAll(); for(Country country:list) { System.out.println(country); } } }