Mybatis项目构建

192 阅读1分钟

Mybatis项目构建 2022.4.8.17.05

*pom.xml导入相应包

image-20220408143534046.png

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>org.example</groupId>
    <artifactId>student_course_spring</artifactId>
    <version>1.0-SNAPSHOT</version>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
​
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.13</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.1</version>
            </dependency>
​
        </dependencies>
​
        <build>
​
            <resources>
                <resource>
                    <!--指定去哪拷贝-->
                    <directory>src/main/java</directory>
                    <includes>
                        <!--指定拷贝什么文件-->
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                    </includes>
                </resource>
                <!--当配置了src/main/java下的就不会拷贝resource下的文件了所以还需要配置一下-->
​
            </resources>
​
        </build>
​
    </project>

1.编写实体类

image-20220408134333637.png

package com.klabsy.pojo;
​
import lombok.Data;
import java.util.Date;
​
@Data
public class Student {
    private int Sno;
    private String Sname;
    private String Ssex;
    private int Sage;
    private String Sdept;
}
​

2.编写核心配置文件

image-20220408143534046.png

<?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>
​
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
​
    <!-- 设置别名(为实体类设置别名) -->
    <typeAliases>
        <!-- 为每一个实体设置别名(实体类) -->
        <!-- 设置实体类包,为该package中的每个实体类自动设置别名 -->
        <package name="com.klabsy.pojo"/>
    </typeAliases>
​
​
​
<!--    被spring里的数据源替代-->
    <environments default="myenv"><!--default值为一个env的id-->
        <environment id="myenv">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost/stu?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
​
​
​
​
    <mappers>
        <!--一个mapper标签指定一个sql映射文件路径从类路径开始classes-->
        <mapper resource="com/klabsy/mapper/StudentMapper.xml"/>
    </mappers></configuration>

3.编写接口

image-20220408143547042.png

4.编写Mapper.xml

image-20220408143519147.png

<?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.klabsy.mapper.StudentMapper">
​
    <select id="selectStudent" resultType="student">
        select * from Student ;
    </select>
    <insert id="insertStudent">
        insert into Student (Sno,Sname,Ssex, Sage, Sdept) values (#{Sno},#{Sname},#{Ssex},#{Sage}, #{Sdept})
    </insert>
    <delete id="deleteStudent">
        delete from Student where Sno = #{Sno}
    </delete>
    <update id="updateStudent">
        update Student set Sname = #{Sname},Ssex=#{Ssex}, Sage=#{Sage}, Sdept=#{Sdept},  where Sno = #{Sno}
    </update>
​
​
​
</mapper>

5.测试

image-20220408143557009.png

import com.klabsy.mapper.StudentMapper;
import com.klabsy.pojo.Student;
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.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
import java.applet.AppletContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
​
public class MyTest {
    @Test
    public void test() throws IOException {
        String resources = "mybatis-config.xml";
        InputStream in = Resources.getResourceAsStream(resources);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sessionFactory.openSession(true);
​
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.selectStudent();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }
}