Springmvc mybatis Foundation

Time:2022-6-4

Mybatis is a persistence layer framework that supports SQL queries and stored procedures. It is flexible in configuration and easy to use. Through the use of XML configuration and JDBC, it is very convenient to operate the database

Configure pom XML

First, introduce the third-party component of mybatis through Maven

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>


<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>


<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>

Create data table

CREATE TABLE 'Student' (
  'Id' int(11) NOT NULL AUTO_INCREMENT,
  'Name' varchar(50) DEFAULT NULL,
  'Code' varchar(50) DEFAULT NULL,
  PRIMARY KEY ('Id')
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf-8

Create table entity

package dto;

public class Student{
    private int Id ;
    public int getId() {
        return Id;
    }
    public void setId(int id) {
        Id = id;
    }

    private String Code;
    public String getCode() {
        return Code;
    }
    public void setCode(String Code) {
        Code = Code;
    }
    
    private String Name ; 
    public String getName() {
        return Name;
    }
    public void setName(String Name) {
        Name = Name;
    }
}

Studentmapper.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="mapping.Studentmapper">
   <!--  Specify query SQL. Parametertype is the type of incoming parameter, and resulttype is the return result -- >
    <select id="getStudent" parameterType="int" resultType="dto.Student">
        SELECT * FROM Student WHERE id=#{id}
    </select>
</mapper>

‘\

  • The’\\
  • The data passed in with the’\\

    int i=1;String s=”s123″;

    Id=\i\and name=\s} —– > id=1 and name=’s123’/ / using \will automatically add single quotation marks” outside the string type

    Id=$i$and name=${s} —– > id=1 and name=s123 / / when $is used, the content is directly replaced without other processing

  • ‘\\

Configure database connection information

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!--  The ERP in the configuration database connection information URL is the data name -- >
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/erp" />
                <property name="username" value="root" />
                <property name="password" value="123456" />
            </dataSource>
        </environment>
    </environments>
    
     <mappers>
        <!--  Specify mapping file -- >
        <mapper resource="mapping/skusMapper.xml"/>
    </mappers>

</configuration>

query

@Controller
@RequestMapping("/Home")
public class HomeController {

    @Resource(name="applePhone")
    private IMobilePhone phone;
    
    @RequestMapping(value="index")
    public String Index()
    {
        String msg=phone.PhoneBrand();
        
        System.out.print(msg);
        
          String resource = "/conf.xml";
            //Load the configuration file for mybatis
            InputStream inputstream =this.getClass().getResourceAsStream(resource);
           
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputstream);
          
            SqlSession session = sessionFactory.openSession();
           
            String statesql= "mapping.Studentmapper.getStudent";        // In studentmapper Namespace + method name in XML
            
            Student student = session.selectOne(statesql, 1);
            System.out.println(student.getName());
            
        return "index";
    }