Introduction to MyBatis of the SSM framework



MyBatis summary

1. Create the project file and configure



the Pmop.xml file 1. Add the mybatis library and the mysql-connector-java library to the configuration file; 

2. Add the

 

   following two files to the configuration file, the function is to let The program can also compile the files in these two folders during the pre-compilation period. The code is as follows:


  



 

 

    MyBatis

  

      src/main/resources

    

       **/*.properties

     

       **/*.xml

     

       **/*.tld

     

      false

    

      src/main /java

    

       **/*.properties

     

       **/*.xml

     

       **/*.tld

     

      false

     

database and table"> Second, after configuring the pom.xml file, create the corresponding database and table.

1. Create a database When creating a table, there are various ways, you can choose terminal operation, you can also use DataBase on Idea, you can also use DataGrip software; 2. Using DataGrip software, the code for creating the table is as follows:



CREATE DATABASE db0602;


USE db0602;


CREATE TABLE student (user_id INTEGER PRIMARY KEY AUTO_INCREMENT,user_name VARCHAR(200),user_des VARCHAR(200));


INSERT INTO student VALUES (NULL,'zhangsan');

INSERT INTO student VALUES (NULL,'lisi');

SELECT *FROM student ;

3. Create a configuration file config.xml file in the resource folder



<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration

        PUBLIC "-//mybatis .org//DTD Config 3.0//EN"

        "https://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

    <!--In this configuration file, all tags All in strict order -->

    <environments default="deve">

        < environment id="deve">

            <transactionManager type="JDBC"/>

            <dataSource type="POOLED">

                <!--四个属性-->

                <property name="driver" value="com.mysql.jdbc.Driver"/>

                <property name="url" value="jdbc:mysql://localhost:3306/db0602"/>

                <property name="username" value="root"/>

                <property name="password" value="1qaz2wsx"/>

            </dataSource>

        </environment>

    </environments>


    <mappers>

        <!--<mapper resource="com/lanou/mapper/StudentMapper.xml"></mapper>-->

        <!--Automatically read all xml files under mapper-- >

        <package name="com.lanou.mapper"/>

    </mappers>


</configuration>

Fourth, create the entity class of the corresponding table, and create the corresponding interface file and xml configuration under the mapper folder File

1. According to the above table, the entity class student code that needs to be created is as follows:



public class Student {

    private Integer id;

    private String name;


}

2. Create the interface file StudentMapper, create the StudentMapper.xml file

*Write in the Mapper file to execute method; *Write a statement to operate the database in the xml file; for example, will write a statement to query all the data in the table.



public interface StudentMapper {


    //Query all students

    List

 

    allListStudent();

}

  



<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper

        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

        "https:/ /mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace: generally write the corresponding interface path-->

<mapper namespace="com.lanou.mapper.StudentMapper"> ;

<select id="allListStudent" resultMap="BaseMap">

        SELECT * FROM student;

    </select>

</mapper> Note: You need to add the corresponding   mapper to the tag    in

config.xml3 .Create a test class to test whether the database can be operated. Create a BatisTest class, the specific code is as follows: public class BatisTest {     @Test

 














    public void test1() throws IOException {


        //1. Load config file

        String re = "config.xml";


        InputStream stream = Resources.getResourceAsStream(re);


        //2. Create SqlSessionFactory

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build (stream);


        //3. Generate SqlSession

        SqlSession session = factory.openSession();


        //4. Get mapper object

        StudentMapper mapper

                 = session.getMapper(StudentMapper.class);


        //5. Call the method of querying all data to get the result

        List

 

    students = mapper.allListStudent();

        System.out.println(students);


        //To modify the database, you need to use session to submit transactions

        session.commit();

        session.close();

    }


}

  

Conclusion: There



is nothing wrong with the running result, it is very useful as a novice to enter the pit.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326402213&siteId=291194637