欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis,从无开始构建StudentDemo

程序员文章站 2022-05-03 23:38:03
配置pom.xml1.添加mysql依赖mysqlmysql-connector-java8.0.152.添加Mybatis依赖...

配置pom.xml
1.添加mysql依赖

<!--mysql依赖-->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.15</version>
</dependency>

2.添加Mybatis依赖

<!--mybatis依赖-->
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>3.4.6</version>
</dependency>

3.添加Junit依赖

<!--单元测试依赖-->
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.13.1</version>
	<scope>test</scope>
</dependency>

4.添加不知名代码段

<build>
	<resources>
		<resource>
			<directory>src/main/resources</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>true</filtering>
		</resource>
		<resource>
			<directory>src/main/java</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>true</filtering>
		</resource>
	</resources>
</build>

配置Mybatis主配置文件(官网拷贝)
代码中以下语句必须配置:

driver
url
username
password
mapper resource
<?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"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

创建StudentMapper接口

public interface StudentMapper {
    int AddStudent(Student student);
}

创建StudentMapper.xml文件(官网拷贝)
代码中以下语句必须配置:

namespace
id
parameterType
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">


<mapper namespace="Mapper.StudentMapper">
    <insert id="AddStudent" parameterType="domain.Student">
        insert into student values(#{id},#{name},#{email},#{age})
    </insert>

</mapper>

根据数据库表设置Student类

public class Student {
    private int id;
    private String name;
    private String email;
    private int age;

    public Student(int id, String name, String email, int age) {
        this.id=id;
        this.name=name;
        this.email=email;
        this.age=age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }
}

设置连接工具类StudentUtils

public class StudentUtils {
    public static SqlSessionFactory sqlSessionFactory;


    static{
        try {
            String resource = "mybatis.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
}

文件框架如下:

Mybatis,从无开始构建StudentDemo

本文地址:https://blog.csdn.net/weixin_48802251/article/details/109821626