MyBatis笔记(一)------从0到1的MyBatis程序


概述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第一个MyBatis程序

1.新建一个普通的maven项目
2.导入maven依赖

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
</dependencies>

3.编写mybatis核心配置文件
在resources文件夹下建立mybatis-config.xml

<?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?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

</configuration>
    

3.编写mybatis工具类

public class MybatisUtils {
    public static SqlSessionFactory sqlSessionFactory;

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

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

}

sqlSessionFactory.openSession(true);
这里的true将事务设置为自动提交,若不设置为自动提交,在sql进行增删改时要手动提交事务

4.编写和数据库对应的实体类
在这里插入图片描述

5.编写mapper(dao)接口

public interface userMapper {
    List<User> getUserList();

    User getUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);


}

6.编写实现userMapper接口的userMapper.xml配置文件(接口类由原来的daoIplm变为一个配置文件)

<?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.govd.dao.userMapper">

    <select id="getUserList" resultType="User">
        select * from mybatis.user
    </select>

    <select id="getUserById" resultType="User">
        select * from user where id=#{id}
    </select>

    <insert id="addUser" parameterType="User">
        insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
    </insert>

    <update id="updateUser" parameterType="User">
        update user set name=#{name},pwd=#{pwd} where id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from user where id=#{id}
    </delete>

</mapper>

配置文件解释
namespace:绑定一个mapper接口
id:对应mapper中的一个方法
resultType:返回的类型
parameterType:参数类型
用#{}来取参数,当传入的的参数为一个普通类型参数时可以直接取出,如果传入的是一个对象,也可以直接取出对象中的属性

7.在mybatis-config.xml中注册对应的mapper

 <mappers>
        <mapper resource="com/govd/dao/userMapper.xml"/>
 </mappers>

如果没有注册mapper会出现如下错误
在这里插入图片描述

8.junit测试

public class userMapperTest {
    @Test
    public void getUserList(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        userMapper mapper = sqlSession.getMapper(userMapper.class);

        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }

        sqlSession.close();
    }

    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        userMapper mapper = sqlSession.getMapper(userMapper.class);

        User userById = mapper.getUserById(1);
        System.out.println(userById);

        sqlSession.close();
    }

    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        userMapper mapper = sqlSession.getMapper(userMapper.class);

       mapper.addUser(new User(4,"李五","13423552"));


        sqlSession.close();
    }

    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        userMapper mapper = sqlSession.getMapper(userMapper.class);

        mapper.updateUser(new User(1,"张三","123456"));


        sqlSession.close();

    }

    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        
        userMapper mapper = sqlSession.getMapper(userMapper.class);

        mapper.deleteUser(4);
        
        sqlSession.close();
    }

可能出现的问题

在这里插入图片描述

在maven中约定大于配置,所以不放在resource下的配置文件可能无法被导出到target中,这时就需要取配置maven的配置文件

文件夹整体:
在这里插入图片描述

在pom.xml中添加

<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、付费专栏及课程。

余额充值