Query users by id
sql used:
SELECT * FROM `user` WHERE id = 1
Add the select tag in user.xml and write 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"> <!-- namespace: Namespace, for isolation sql,There is also a very important role, which will be discussed later. --> <mapper namespace="test"> <!-- id:statement Of id Or called sql Of id--> <!-- parameterType:Declare the type of input parameter --> <!-- resultType:Declare the type of output result, which should be filled in pojo Full path --> <!-- #{}: placeholder for input parameter, equivalent to jdbc's? > <select id="queryUserById" parameterType="int" resultType="cn.itcast.mybatis.pojo.User"> SELECT * FROM `user` WHERE id = #{id} </select> </mapper>
Test procedure:
Test procedure steps:
1. Create SqlSessionFactoryBuilder object
2. Load SqlMapConfig.xml configuration file
3. Create SqlSessionFactory object
4. Create SqlSession object
5. Execute SqlSession object to execute query and get the result User
6. Print results
7. Release resources
public class MybatisTest { private SqlSessionFactory sqlSessionFactory = null; @Before public void init() throws Exception { // 1. Create SqlSessionFactoryBuilder object SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // 2. Load SqlMapConfig.xml configuration file InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); // 3. Create SqlSessionFactory object this.sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); } @Test public void testQueryUserById() throws Exception { // 4. Create SqlSession object SqlSession sqlSession = sqlSessionFactory.openSession(); // 5. Execute the SqlSession object to query and get the result User. // The first parameter is the id of the statement of User.xml, and the second parameter is required for executing sql. Object user = sqlSession.selectOne("queryUserById", 1); // 6. Print results System.out.println(user); // 7. Release resources sqlSession.close(); } }
Fuzzy query of users based on user name
Query sql:
SELECT * FROM `user` WHERE username LIKE '% Wang%'
Method 1: add the following content to the User.xml configuration file:
<!-- If multiple results are returned, mybatis Will automatically put the returned results in the list Container --> <!-- resultType The configuration of is the same as that of returning a result. --> <select id="queryUserByUsername1" parameterType="string" resultType="cn.itcast.mybatis.pojo.User"> SELECT * FROM `user` WHERE username LIKE #{username} </select>
@Test public void testQueryUserByUsername1() throws Exception { // 4. Create SqlSession object SqlSession sqlSession = sqlSessionFactory.openSession(); // 5. Execute the SqlSession object to query and get the result User. // Query multiple data using the selectList method List<Object> list = sqlSession.selectList("queryUserByUsername1", "%king%"); // 6. Print results for (Object user : list) { System.out.println(user); } // 7. Release resources sqlSession.close(); }
Method two:
Add the following to the User.xml configuration file
<!-- If the parameter passed in is a simple data type, ${}It has to be written value --> <select id="queryUserByUsername2" parameterType="string" resultType="cn.itcast.mybatis.pojo.User"> SELECT * FROM `user` WHERE username LIKE '%${value}%' </select>
@Test public void testQueryUserByUsername2() throws Exception { // 4. Create SqlSession object SqlSession sqlSession = sqlSessionFactory.openSession(); // 5. Execute the SqlSession object to query and get the result User. // Query multiple data using the selectList method List<Object> list = sqlSession.selectList("queryUserByUsername2", "king"); // 6. Print results for (Object user : list) { System.out.println(user); } // 7. Release resources sqlSession.close(); }