其他分享
首页 > 其他分享> > Mybatis

Mybatis

作者:互联网

1.第一个Mybatis程序

思路:搭建环境---》导入MyBatis---》编写代码----》测试

2.1搭建环境

搭建数据库

CREATE DATABASE

use mybatis;
CREATE TABLE user(
id INT(20) not null PRIMARY KEY,
`name` VARCHAR(30) DEFAULT null,
pwd VARCHAR(20) DEFAULT NULL
)

INSERT INTO user(id,`name`,pwd) VALUES
	( 1, '赖 敏', '123456' ),
	( 2, '吴瑞红', '123896' ),
	( 3, '来院系', '1245456' )

新建项目

1.新建一个普通的·maven项目

2.删除src目录

3.导入maven依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.5</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
</dependency>

2.2创建一个模块

编写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">
<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/mybatis?useLL=true&amp;useuUnicode=true;cahracterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    </mappers>
</configuration>

编写mybatis工具类

private static SqlSessionFactory sqlSessionFactory;

static {
    try {
        String resource = "org/mybatis/example/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public static SqlSession getSqlSession(){
    return sqlSessionFactory.openSession();
    
}

2.3编写代码

实体类

package com.lm.pojo;

public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    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 getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

Dao接口

public interface UserDao {

    List <User> getUserList();

    }

接口实现类由原来的UserDaoImpl转换为一个mapper文件

<?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.lm.dao.UserDao">
    <select id="getUserList" resultType="com.lm.pojo.User">
    select * from mybatis.user
  </select>
</mapper>

2.4 测试

标签:String,public,mybatis,pwd,Mybatis,id,name
来源: https://www.cnblogs.com/lyxyyds/p/16519646.html