一.创建 Java 工程:

二.创建数据库和 User 表

创建所需的数据库和表,要创建的数据库是:mybtest,并在 yiibai 数据库创建一个表:user
数据库.jpg
三.创建 Mybatis 配置文件

<?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">
  <!--mybatis-3-config.dtd:约束规范,限定当前xml文件能够写哪些节点  -->
<configuration>
   <!-- 数据源环境-->
    <environments default="development">
        <environment id="development">
            <!-- 事务管理方式  -->
            <transactionManager type="JDBC" />
            <!--数据源连接属性  -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybtest" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <!-- 管理系统中的映射文件 -->
    <mappers>
        <mapper resource="com/offcn/bean/UserMapping.xml" />
    </mappers>
</configuration>

四.创建实体类

import java.util.Date;

public class User {
    private int id;
    private String uername;
    private Date birthday;
    private String sex;
    private String address;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUername() {
        return uername;
    }
    public void setUername(String uername) {
        this.uername = uername;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public User(int id, String uername, Date birthday, String sex, String address) {
        super();
        this.id = id;
        this.uername = uername;
        this.birthday = birthday;
        this.sex = sex;
        this.address = address;
    }
    public User(String uername, Date birthday, String sex, String address) {
        super();
        this.uername = uername;
        this.birthday = birthday;
        this.sex = sex;
        this.address = address;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", uername=" + uername + ", birthday=" + birthday + ", sex=" + sex + ", address="
                + address + "]";
    }
    
}

五.创建映射文件

<?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">
 <!--mybatis-3-mapper.dtd:约束规范,限定当前xml文件能够写哪些节点  -->
 <!--namespace:命名空间,可以任意定义,用来打包sql语句的  -->
<mapper namespace="org.mybatis.example.BlogMapper">
    <!--id:sql语句的唯一标志,在同一个命名空间中id不能够重复,resultType:返回结果(执行sql语句后要将结果集映射到哪个对象上)  -->
    <!-- #{}相当于占位符?如果parameterType输入参数 -->
    <select id="selectUser" resultType="com.offcn.bean.User">
        select * from user where id = #{id}
    </select>
</mapper>

六.运行结果

jieguo.jpg

Last modification:September 19th, 2019 at 12:09 am
如果觉得我的文章对你有用,请随意赞赏