0


创建一个Mybatis工程

1:首先,设计好框架:

请添加图片描述

2:写工具类:

packagecom.ftz.Demo.utils;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importjava.io.IOException;importjava.io.InputStream;publicclassMybatisUtil{privatestaticSqlSessionFactory sqlSessionFactory;static{try{String resource ="mybatis-config.xml";InputStream inputStream =Resources.getResourceAsStream(resource);
            sqlSessionFactory =newSqlSessionFactoryBuilder().build(inputStream);}catch(IOException e){
            e.printStackTrace();}}publicstaticSqlSessiongetSqlSession(){return sqlSessionFactory.openSession();}}

请添加图片描述

3:配置文件:

请添加图片描述

<?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.cj.jdbc.Driver"/>
                <property name="url"
                          value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT%2B8&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/ftz/Demo/dao/userMapper.xml"/>
    </mappers>
</configuration>

4:写实体类:

请添加图片描述

packagecom.ftz.Demo.po;importlombok.Data;@DatapublicclassUser{privateint id;//idprivateString name;//姓名privateString pwd;//密码}

5:写接口:

请添加图片描述

packagecom.ftz.Demo.dao;importcom.ftz.Demo.po.User;importjava.util.List;publicinterfaceUserMapper{List<User>getUserList();}

6:写Mapper.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 namespace="com.ftz.Demo.dao.UserMapper"><select id="getUserList" resultType="com.ftz.Demo.po.User">
        select * from mybatis.user
    </select></mapper>

7:测试:

请添加图片描述

packagecom.ftz.Demo.dao;importcom.ftz.Demo.po.User;importcom.ftz.Demo.utils.MybatisUtil;importorg.apache.ibatis.session.SqlSession;importorg.junit.Test;importjava.util.List;publicclassUserDaoTest{@Testpublicvoidtest(){SqlSession sqlSession =MybatisUtil.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);List<User> userList = mapper.getUserList();for(User user : userList){System.out.println(user);}
        sqlSession.close();}}

注意:配置文件的过滤:在pom里面写:

请添加图片描述

<build><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include>
                    <include>**/*.properties</include></includes><filtering>true</filtering></resource><resource><directory>src/main/resources</directory><includes><include>**/*.xml</include>
                    <include>**/*.properties</include></includes><filtering>true</filtering></resource></resources></build>

请添加图片描述


本文转载自: https://blog.csdn.net/justleavel/article/details/122706581
版权归原作者 夜色架构师 所有, 如有侵权,请联系我们删除。

“创建一个Mybatis工程”的评论:

还没有评论