MybatisDay02
-
自定义一个类型转换器(讲java转换到数据库, 讲数据库转换到java)
-
第一步编写一盒转换器类,实现 BaseTypeHandler<Date> 泛型是你要转换的类型,并且实现他的四个方法
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException { long time = date.getTime(); preparedStatement.setLong(i,time); } //将数据库中类型 转换成java类型 //String参数 要转换的字段名称 //ResultSet 查询出的结果集 public Date getNullableResult(ResultSet resultSet, String s) throws SQLException { //获得结果集中需要的数据(long) 转换成Date类型 返回 long aLong = resultSet.getLong(s); Date date = new Date(aLong); return date; } //将数据库中类型 转换成java类型 public Date getNullableResult(ResultSet resultSet, int i) throws SQLException { long aLong = resultSet.getLong(i); Date date = new Date(aLong); return date; } //将数据库中类型 转换成java类型 public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException { long aLong = callableStatement.getLong(i); Date date = new Date(aLong); return date;
-
第二步:去核心配置文件配置
<!--注册类型处理器--> <typeHandlers> <typeHandler handler="com.itheima.handler.DateTypeHandler"></typeHandler> </typeHandlers>
-
第三步:测试
-
pageHelper 分页! 超级简单
-
通过PageHelper这个插件来分页
-
第一步:导入坐标
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5</version> </dependency>
-
第二步:现创建一个查询全部人的方法, 并且在映射文件中写好查询所有人的 sql语句
-
第三步:在核心配置文件配置插件
<!--配置分页助手插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins>
-
第四步:编写程序测试
public void test3() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); //设置分页相关参数 当前页+每页显示的条 PageHelper.startPage(2,3);//第一页,每页三条数据 List<User> userList = mapper.findAll(); for (User user : userList) { System.out.println(user); } //泛型是你要分页的类型 传入的参数你查询的结果 PageInfo<User> pageInfo = new PageInfo<User>(userList); System.out.println("当前页="+pageInfo.getPageNum()); System.out.println("每页数据="+pageInfo.getPageSize()); System.out.println("上一页="+pageInfo.getPrePage()); System.out.println("下一页="+pageInfo.getNextPage()); System.out.println("是否是第一页:"+pageInfo.isIsFirstPage()); System.out.println("是否是最好一页"+pageInfo.isIsLastPage()); System.out.println("一共多少页="+pageInfo.getPages()); System.out.println("一共多少条数据="+pageInfo.getTotal()); System.out.println("得到最后一页="+pageInfo.getLastPage()); sqlSession.close(); }
-
多表查询 (一对一)
-
先创建一个数据库表Order 和User Order中外键uid是User表中的主键id
-
方法一:
-
第一步:先创建Order实体类
注意uid用注入
private User user;
-
第二步:创建OrderMapper接口
-
第三步:创建一个findAll的方法,返回值是List《Order》
-
第四步:配置OrderMapper映射器
<mapper namespace="com.itheima.mapper.OrderMapper"> <resultMap id="orderMap" type="order"> <!--主键用id标签--> <!--column中是数据库的字段名,下面sql语句为他起了oid的别名,所以可以写oid--> <!--property是Order的属性名字 整个标签翻译为,讲oid赋给id--> <id column="oid" property="id"></id> <result column="ordertime" property="ordertime"></result> <result column="total" property="total"></result> <!--order属性中的user 需要我们手动帮他赋值,不然 我们查询到的username等都是去order里面找一样的属性名username ,但是我们只有user所以无法查询到user表信息--> <result column="uid" property="user.id"></result> <result column="username" property="user.username"></result> <result column="password" property="user.password"></result> </resultMap> <select id="findAll" resultMap="orderMap" > select *,o.id oid from orders o,user u where o.uid = u.id </select> </mapper>
-
配置核心配置文件:注意要导入OrderMapp.xml映射文件 还有为他命名为order
-
第六步:测试
OrderMapper mapper = sqlSession.getMapper(OrderMapper.class); List<Order> orderList = mapper.findAll(); for (Order order : orderList) { System.out.println(order); } sqlSession.close();
方法二:
-
知识OrderMapper .xml的文件修改了一点
<mapper namespace="com.itheima.mapper.OrderMapper"> <resultMap id="orderMap" type="order"> <!--主键用id标签--> <!--column中是数据库的字段名,下面sql语句为他起了oid的别名,所以可以写oid--> <!--property是Order的属性名字 整个标签翻译为,讲oid赋给id--> <id column="oid" property="id"></id> <result column="ordertime" property="ordertime"></result> <result column="total" property="total"></result> <!--property是order中的 属性名,javaType是该属性名的类型--> <association property="user" javaType="user"> <!--这里就可以直接讲从数据库查找的数据注入给user 不用在user.什么的了--> <id column="uid" property="id"></id> <result column="username" property="username"></result> <result column="password" property="password"></result> </association> </resultMap> <select id="findAll" resultMap="orderMap" > select *,o.id oid from orders o,user u where o.uid = u.id </select>
感觉方法二 更好理解
-
-
多表查询 (一对多)
-
和上面的一样(一个用户可以有多个订单号,一个订单号只能有一个用户) 我们需要在user类中注入属性List《Order》,因为order是对象且有多个 所以用集合
-
第一步:先创建一个UserMapper接口 里面有一个FindAll 的方法来查询所有user 并且在查询是打印除用户所有order
-
第二步:编写UserMapper.xml文件
<resultMap id="userMap" type="user"> <id column="uid" property="id"></id> <result column="username" property="username"></result> <result column="password" property="password"></result> <collection property="orderList" ofType="order" > <id column="oid" property="id"></id> <result column="total" property="total"></result> <result column="ordertime" property="ordertime"></result> </collection> </resultMap> <select id="findAll" resultMap="userMap"> select *,o.id oid from orders o,user u where o.uid = u.id; </select> </mapper>
sql语句跟一对一的一样,传值的语句也差不多,只不过我们这里是集合,所以变成了collection标签, javatype变成了ofType,其余不变
-
第三:最后测试
-
多表查询 (多对多)
-
一个用户有多个角色,一个角色能被多个人拥有
-
第一步:像创建一个Role类
-
第二步:在user类中注入List《Role》属性 (和前面的order一样),并且在接口创建方法
-
第三步: 在UserMapper.xml文件配置
<!--这里查询所有用户以及他的角色--> <resultMap id="userRoleMap" type="user"> <id column="userId" property="id"></id> <result column="username" property="username"></result> <result column="password" property="password"></result> <collection property="roleList" ofType="role"> <id column="roleId" property="id"></id> <result column="roleName" property="roleName"></result> <result column="roleDesc" property="roleDesc"></result> </collection> </resultMap> <select id="findAllUserAndRole" resultMap="userRoleMap"> select * from user u,sys_user_role ur,sys_role r where u.id = ur.userId and ur.roleId = r.id </select>
-
第四步:测试
-
-
总结!
-
一对一:
使用
<resultMap > <association </resultMap>
-
一对多:
使用
<resultMap > <collection </resultMap>
-
多对多
使用
<resultMap > <collection </resultMap>
-
采用注解实现一对一 ,一对多, 多对多的查询
-
第一步:现在核心配置文件中配置(一对一)
<!--加载映射关系--> <mappers> <!--指定接口所在的包--> <package name="com.itheima.mapper"></package> </mappers>
-
第二步:编写接口 和方法
@Select(" select *,o.id oid from orders o ,user u where o.uid = u.id") @Results({ @Result(column = "oid",property = "id"), @Result(column = "ordertime",property = "ordertime"), @Result(column = "total",property = "total"), @Result(column = "uid",property = "user.id"), @Result(column = "username",property = "user.username"), @Result(column = "password",property = "user.password") }) public List<Order> findAllRoleAndUser(); @Insert("insert into orders(ordertime,total,id)values(#{ordertime},#{total},#{id})") public void addOrder(Order order);
-
第四步:测试
-
-
一对多
-
采用分表参训,首先 我们先查询user表,再通过查到的id 去查询orders表
-
第一步:我们在userMapper 接口编写方法
-
第二步:采用注解的方式查询user表
//一对多表查询 @Select("select * from user") //将得到的结果带入继续查询 @Results({ @Result(id = true ,column = "id" , property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), }) public List<User> findAllUserAndRole();
-
第三步:我们在通过查询到的id查询orders表 (注意! 这个时候我们查询到的id已经是值了,所以我们首先要去orderMapper接口创建一个查询的方法,然后将我们刚才查到的 id给这个方法)
@Select("select * from orders o where o.uid = #{uid} ") public List<Order> findById(int uid);
-
第四步:在userMapper 接口调用这个方法 并且将id给他
//一对多表查询 @Select("select * from user") //将得到的结果带入继续查询 @Results({ @Result(id = true ,column = "id" , property = "id"), @Result(column = "username",property = "username"), @Result(column = "password",property = "password"), @Result( property = "orderList", //order集合的属性名 column = "id", //上面的查询结果得到的值 javaType = List.class, //查询类型 many = @Many(select = "com.itheima.mapper.OrderMapper.findById") ) }) public List<User> findAllUserAndRole();
-