MyBatis配置实现方法

根据mybatis官方文档配置Mybatis

1.实体类

2.UserDao(UserMapper)接口类

3.副配置文件(相当于UserDaoImpl类)

<mapper namespace="com.sgl.dao.UserMapper">
    <select id="getUserList" resultType="com.sgl.pojo.User">
        select * from User
    </select>
</mapper>

namespace的配置相当于原来的UserDaoImpl中实现了UserDao接口,由于这里的UserDao类改为了UserMapper类,所以namespace的值为com.sgl.dao.UserMapper,实现此mapper与接口类的绑定。
select标签中的id属性为UserMapper类中的接口方法,resultType为返回的类型,不论返回是集合还是单个User,resultType都为User的全限定类名。类似参数还有parameterType,指的是参数类型。select标签中为select语句。
增删改操作类似。

4.MybatisUtils工具类

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

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

}}

简化操作,不用每次都写重复代码。

5.主配置文件

<?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>
    <properties resource="db.properties"></properties>
    <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="com/sgl/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>

此处引用了db.properties的数据库配置文件。
数据库配置文件部分参数,由于使用了mysql8:

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db1?serverTimezone=UTC&characterEncoding=utf-8

6.进行测试

		SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }

注:为防止maven写入失败,在pom文件中加入下列代码:

    <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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值