MyBatis - 基础学习1 - 第一个mybatis程序

第一步:编写我们使用数据库的工具类

// sqlSessionFactory  -- > sqlSession
public class MybatisUtils {
   private static SqlSessionFactory sqlSessionFactory;
    private static InputStream inputStream = null;
    static {
        try {
            //使用mybatis第一步,获取sqlSessionFactory
            String resource = "mybatis-config.xml";
            inputStream = Resources.getResourceAsStream(resource);
             sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession(){
       return sqlSessionFactory.openSession();
    }

}

 

第二步:由于工具类中需要读取我们数据库的位置,用户名,密码,所以我们又在resources目录下写了一个资源xml供工具类登录mysql

<?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核心配置文件-->
<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?useSSL=false&amp;uesUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!--每一个mapper.xml都需要注册,mybatis核心配置文件中注册-->
    <mappers>
        <mapper resource="top/lostyou/dao/UserMspper.xml"/>
    </mappers>
</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 + '\'' +
                '}';
    }
}

 

第四步:我们使用mybaits要怎样操作数据库(增删改查),在Dao包下写一个xml用来写怎样操作数据库

<?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接口-->
<mapper namespace="top.lostyou.dao.UserDao">

<select id="getUserList" resultType="top.lostyou.pojo.User">
    SELECT * from mybatis.user WHERE id=3
</select>

</mapper>

 

第五步:实例化我们操作数据库的xml,并用一个方法作为调用xml操作数据库的引线

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

 

第六步:正式开始测试我们的mybatis程序是否可以完成对数据库的操作

public class UserDaoTest {
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //第一种执行sql方式:getMapper
        try {


        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }}catch (Exception e){
            System.out.println("出错了");
            e.fillInStackTrace();
        }finally {
            //关闭sqlsession
            sqlSession.close();
        }

    }
}

 

注意点:使用maven可能面对,导不出resources包的资源文件所以我们需要在pom.xml中完成一下配置

    <!--在build中配置resources,防止资源导出失败问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

 

posted @ 2022-11-14 21:08  回忆也交给时间  阅读(20)  评论(0编辑  收藏  举报