首页 > 编程语言 >2.第一个MyBatis程序

2.第一个MyBatis程序

时间:2023-01-04 00:55:36浏览次数:43  
标签:xml 第一个 程序 id String pwd MyBatis public name

1.搭建环境

(1)搭建数据库 

create database mybatis;
use mybatis;
create table user(
    id int(20) PRIMARY KEY,
    name VARCHAR(20) NOT NULL,
    pwd VARCHAR(20) NOT NULL
		);
INSERT INTO USER VALUES
		(1,"zuo","123465"),
        (2,"xiao","123465"),
        (3,"lisi","123465");

 (2)新建项目(父工程)
       新建一个普通的maven项目
       删除src文件夹,形成一个父工程
       导入maven依赖(jar包)

 (3)创建一个模块(子工程)

  编写MyBatis工具类

public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        //使用MyBatis第一步获取sqlSessionFactory对象
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
    // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

   编写mybatis-config.xml配置文件

<?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>
    <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://127.0.0.1:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

  编写代码

    实体类

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;}
    @Override
    public String toString() {
        return "User{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", pwd='" + pwd + '\'' +
            '}';
    }
}

    Dao接口

public interface userDao {
    List<User> getUserList();
}

    实现类由UserDaoImpl转换为UserMapper.xml配置文件

<?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命名空间 绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.zuo.dao.userMapper">
    <select id="getUserList" resultType="com.zuo.entity.User">
        select * from mybatis.user
    </select>
</mapper>

   测试

public class UserDaoTest {
    @Test
    public void test(){
        //1.获取sqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取接口
        userDao mapper = sqlSession.getMapper(userMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user:userList) {
            System.out.println(user);
        }
        //关闭sqlSession对象
        sqlSession.close();
    }
}

遇到的问题及解决方法

1.org.apache.ibatis.binding.BindingException: Type interface com.zuo.dao.userDao is not known to the MapperRegistry.    
   解决方法:
       在mybatis-config.xml中配置Mapper.xml

<!--每一个Mapper.XML 都需要在Mybatis核心配置文件中注册!-->
<mappers>
    <mapper resource="com/zuo/dao/UserMapper.xml"/>
</mappers>

2.java.lang.ExceptionInInitializerError----->资源导出失败问题
   解决方法:
         在每个pom.xml文件中配置

<!--在build中配置resources,来防止我们资源导出失败的问题-->
<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>

 

标签:xml,第一个,程序,id,String,pwd,MyBatis,public,name
From: https://www.cnblogs.com/zuok/p/17023769.html

相关文章