首页 > 其他 > 详细

mybatis01

时间:2021-06-05 18:21:22      阅读:15      评论:0      收藏:0      [点我收藏+]

MyBatis-01 踩坑日记

首先整个项目的流程如下

plus:idea读错要从后往前读

 技术分享图片

Bug

1.防止资源导出失败的代码,此代码在pom.xml文件中,作用是解决项目无法正常生成target文件的情况

 

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

2.Idea有时候会不能读取maven项目导入的包,勾选上delegate IDE build..这个选项

 技术分享图片

3.此代码解决File encoding has not been set报错,pom.xml文件中

<properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

4.不绑定此map会有bind报错 注意是/不是. map放在mybatis-config.xml文件的configuration标签中,用于绑定每个mapper的地址

    <mappers>

        <mapper resource="com/wu/dao/UserMapper.xml"/>

    </mappers>

5.pom.xml文件中用于解决 fail to execute org.apache.maven.plugins:maven-surefire-plugin:2.12.4

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surefire-plugin</artifactId>

        <version>2.4.2</version>

        <configuration>

            <skipTests>true</skipTests>

        </configuration>

    </plugin>
</plugins>

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

mybatis-config配置信息

<?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=true&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
                <property name="username" value="root" />
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
<!--不绑定此map会有bind报错 注意是/不是.-->
    <mappers>
        <mapper resource="com/wu/dao/UserMapper.xml"/>
    </mappers>
</configuration>

mybatis的工具类

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        //获取sqlsessionfactory对象
        String resource="mybatis-config.xml";
        InputStream inputStream= null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
       sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

 

mybatis01

原文:https://www.cnblogs.com/wuyimin/p/14852604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!