20200107——day02 mybatis

Summary
sqlsessionfactorybuild accepted sqlmapconfig.xml file stream, constructed sqlsessionfactory objects

and database connections read sqlmapconfig.xml sqlsessionfactory mapper mapping information used to produce the real operation of the database objects sqlsession

There are two objects sqlsession effect 1) generates the interface proxy object 2) define a common CRUD

In the method of getMapper sqlsessionImpl object is achieved in two steps, a first, first read information creating database connection with the connection object sqlsessionfactory. Secondly, by creating a proxy object proxy mode jdk getMapper as the return value, where the main job is to process the third parameter obtained inside the class when creating a proxy object sql statement, perform crud operations.

Package result set, we all need the result set returned by the database are encapsulated into java object is returned to the caller, the caller must know we have the required return type.

Insert Table

  @Test
    public void testSave() throws  Exception{
        User user = new User();
        user.setAddress("吉林长春");
        user.setUsername("马铭泽");
        user.setSex("男");
        user.setBirthday(new Date());

        userDao.saveUser(user);
        sqlSession.commit();

    }
    <insert id="saveUser" parameterType="com.mmz.domain.User" >
        insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday});
    </insert>

Finally, do not forget to add sqlseesion.commit placed in @After

package com.mmz.test;

import com.mmz.dao.UserDao;
import com.mmz.domain.User;
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.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @Classname MybatisTest
 * @Description TODO
 * @Date 2020/1/7 12:21
 * @Created by mmz
 */
public class MybatisTest {
    private InputStream inputStream;
    private SqlSession sqlSession;
    private UserDao userDao;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSessionFactoryBuilder sqlSessionFactoryBuilder;

    @Before
    public void init() throws Exception{
        inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建工厂
        sqlSessionFactoryBuilder  = new SqlSessionFactoryBuilder();
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //3.使用工厂生产对象
        sqlSession =  sqlSessionFactory.openSession();
        //4.使用sqlsessio,创建Dao接口的代理对象
        userDao = sqlSession.getMapper(UserDao.class);
    }
    @After
    public void destroy()throws  Exception{
        sqlSession.commit();
        sqlSession.close();
        inputStream.close();
    }
    @Test
    public void testFind(){
        List<User> users = userDao.findAll();
        for (User user:users
        ) {
            System.out.println(user);
        }
    }

    @Test
    public void testSave() throws  Exception{
        User user = new User();
        user.setAddress("吉林长春");
        user.setUsername("马铭泽");
        user.setSex("男");
        user.setBirthday(new Date());

        userDao.saveUser(user);

    }

    @Test
    public void testUpdate() throws  Exception{
        User user = new User();
        user.setId(51);
        user.setAddress("吉林长春");
        user.setUsername("咕噜咕噜");
        user.setSex("男");
        user.setBirthday(new Date());

        userDao.updateUser(user);
    }
    @Test
    public void testDetele() throws Exception{
        userDao.deleteUser(48);
    }

    @Test
    public void testFindById() throws Exception{
        User user  = userDao.findById(52);
        System.out.println(user);
    }

    @Test
    public void testFindByName() throws Exception{
        List<User> users = userDao.findByName("%小%");
        System.out.println(users);
    }
}

Code on CRUD are here

selectKey
Here Insert Picture Description

mybatis parameters

ognl expression
object graphic navigation language
object graph navigation language

It is a method to retrieve data objects by value, to get on the wording to omit
example, we get the user name
user.getUsername
OGNL expression written user.username
the mybatis in, why can write username directly, rather than user. It
because in parameterType it has been provided class property belongs to, so in this case does not need to write the name of the object
passed pojo wrapped object

Problems arise
when the field if we query the mapped pojo type of inconsistency, how do
the first method as

to solve the problem at the level of sql statement

The second is resultMap, with the configuration of the embodiment
Here Insert Picture Description
id attribute resultMap is to give the latter reference, type is mapped to an object pojo
result of a property tag corresponding to the column names in pojo, colunn corresponds field name in the database
id empathy

properties property
by the property resource attribute, or property is defined in the interior

url attribute, according to the wording of the url to write
url: uniform resource locator Uniform Resource Locator is a unique position that it can identify resource of
its wording:
protocol host port url

Alias typeAliases
can only configure domain aliases
small package name and write permissions name
Here Insert Picture Description

package is used to specify an alias packet, when specified, the entity class will alias register in the packet, the class name is an alias
Here Insert Picture Description

Published 657 original articles · won praise 39 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/103873401