idea 创建mybatis maven项目
环境: idea2019.2.4 jdk11
创建过程
导入 pom.xml
<!--单元测试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--mybatis版本-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<build>
<!-- 如果不添加此节点**.xml,**.properties文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
创建db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123
创建SqlMapConfig.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>
<!--引入配置文件-->
<properties resource="db.properties"/>
<!-- 类型别名 -->
<typeAliases>
<package name="com.panshi.mybatis.pojo"/>
</typeAliases>
<!--和spring整合后取消-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--扫描包下的所有xml文件-->
<package name="com.panshi.mybatis.mapper"/>
</mappers>
</configuration>
创建log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
创建pojo类
Public class Items {
private Integer id;
private String username;
private Integer age;
/**get()/set()/toString()**/
创建ItemMapper.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.panshi.mybatis.mapper.ItemMapper">
<select id="findItemById" resultType="com.panshi.mybatis.pojo.Items" parameterType="int">
select id,username,age from items where id = #{id}
</select>
<insert id="insert" parameterType="Items">
insert into items values(null,#{username},#{age});
</insert>
</mapper>
创建ItemMapper
public interface ItemMapper {
Items findItemById(Integer id);
void insert(Items item);
}
测试类
public class MybatisTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
}
@Test
public void findItemByIdTest() {
SqlSession sqlSession = sqlSessionFactory.openSession();
Items item = sqlSession.selectOne("com.panshi.mybatis.mapper.ItemMapper.findItemById", 1);
System.out.println(item);
sqlSession.close();
}
@Test
public void insert(){
SqlSession sqlSession = sqlSessionFactory.openSession();
Items item = new Items();
item.setUsername("开元");
item.setAge(4);
sqlSession.insert("com.panshi.mybatis.mapper.ItemMapper.insert",item);
sqlSession.commit();
sqlSession.close();
}