MyBatis笔记----MyBatis查询表全部的两种方法:XML与注解
查询单条信息的在
http://www.cnblogs.com/tk55/p/6659285.html 已经有了
XML
修改UserMapper.xml
<?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.ij34.mybatis.UserMapper"> <resultMap type="com.ij34.model.User" id="resultListUser"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="age" property="age"/> </resultMap> <select id="selectUser" resultMap="resultListUser" > select * from users; </select> </mapper>
修改Test.java
package com.ij34.bean; 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 com.ij34.model.User; public class Test { public static void main(String[] args) throws IOException { String resource ="com/ij34/mybatis/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession(); try { List<User> users=session.selectList("com.ij34.mybatis.UserMapper.selectUser"); // UserMapper mapper=session.getMapper(UserMapper.class); // User user=mapper.selectUser(1);; for(User user:users){ System.out.println(user); } } finally { // TODO: handle finally clause session.close(); } } }
结果
注解
UserMapper.java
package com.ij34.model; import java.util.List; import org.apache.ibatis.annotations.Select; public interface UserMapper { @Select("select * from users") public List<User> selectUser(); }
mybatis-config.xml
<mappers>
<mapper class="com.ij34.model.UserMapper"/>
</mappers>
Test.java
package com.ij34.bean; 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 com.ij34.model.User; import com.ij34.model.UserMapper; public class Test { public static void main(String[] args) throws IOException { String resource ="com/ij34/mybatis/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession(); try { // List<User> users=session.selectList("com.ij34.mybatis.UserMapper.selectUser"); UserMapper mapper=session.getMapper(UserMapper.class); List<User> users=mapper.selectUser(); for(User user:users){ System.out.println(user); } } finally { // TODO: handle finally clause session.close(); } } }
结果
--------------------------------------------------------------------------------------------------------------------------------------------
作者:Honey_Badger —— 觉得这文章好,点一下左下角
出处:http://tk55.cnblogs.com/
posted on 2017-04-03 02:17 Honey_Badger 阅读(17044) 评论(0) 编辑 收藏 举报