mybatis01
Posted 一拳超人的逆袭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis01相关的知识,希望对你有一定的参考价值。
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&useUnicode=true&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的主要内容,如果未能解决你的问题,请参考以下文章
MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段