MyBatis快速入门——第一章、idea的maven配置与demo实例
1、环境要求:
1、idea,这里用的版本是:IntelliJ IDEA 2020.1.3 x64 2、mysql,这里的版本5.6版本 3、maven,3.8.x
2、maven引入
pom.xml
代码语言:javascript复制 <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
mybatis-config.xml头部
代码语言:javascript复制<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
mybatis-config.xml正文
代码语言:javascript复制<configuration>
<!-- 加载log4j -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 配置匿名包 -->
<typeAliases>
<package name="com.item.model"></package>
</typeAliases>
<!-- 数据库配置 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mytest?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="12345678"></property>
</dataSource>
</environment>
</environments>
<!-- 引入配置文件列表 -->
<mappers>
<mapper resource="com/item/mapper/UsersMapper.xml"></mapper>
</mappers>
</configuration>
modelMapper.xml头部
代码语言:javascript复制<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
modelMapper.xml正文
代码语言:javascript复制<mapper namespace="com.item.model.Users">
<select id="GetInfo" resultType="Users">
select * from users
</select>
</mapper>
log4j.properties配置
代码语言:javascript复制#全局配置
log4j.rootLogger=debug,stdout,ERROR
#MyBatis日志配直
log4j.logger.com.item.mapper=TRACE
#控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3、创建测试数据库【mytest】
代码语言:javascript复制DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`createDate` datetime(0) NOT NULL,
`nickName` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`sex` int(1) NOT NULL,
`age` int(3) NOT NULL,
`introduce` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci;
INSERT INTO `users` VALUES (1, '2022-05-08 20:52:07', '王语嫣', 0, 16, '燕子坞学霸');
INSERT INTO `users` VALUES (2, '2022-05-08 22:06:24', '赵灵儿', 0, 15, '女娲传人');
INSERT INTO `users` VALUES (3, '2022-05-08 22:06:59', '龙姑娘', 0, 18, '冰山美人');
4、创建项目层级
com.item.model下创建数据库对应的模型类
com.item.action下创建测试类
resources下创建【com.item.mapper/UsersMapper.xml】、【log4j.properties】、【mybatis-config.xml】并根据前三个目录内容编写对应配置文件。
5、编辑代码
【Users实体类】
代码语言:javascript复制package com.item.model;
import java.util.Date;
public class Users {
private int id;
private Date createDate;
private String nickName;
private int sex;
private int age;
private String introduce;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
@Override
public String toString() {
return "Users{"
"id=" id
", createDate=" createDate
", nickName='" nickName '''
", sex=" sex
", age=" age
", introduce='" introduce '''
'}';
}
}
【action测试类】
代码语言:javascript复制package com.item.action;
import com.item.model.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
public class Action {
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sessionFactory.openSession();
List<Users> lists = sqlSession.selectList("GetInfo");
//输出
System.out.println(lists);
sqlSession.close();
}
}
执行成功:
可以看到log4j的日志与对应的数据输出