标签:domain ble tty efault category you Opens 生成 rop
Mybatis是需要完成相应配置文件的配置,只需要接口,不用写sql的具体实现类,就可以完成将SQL的查询结果自动注入到指定对象中,极大简化了开发,减少开发中出错的可能。
一、创建一个maven项目
在pom.xml导入相关依赖
导入Mybatis依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
导入相关依赖,这里我需要用到jdbc,日志,junit
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
二.创建数据库表
CREATE TABLE
user
(
id
int(11) NOT NULL auto_increment,
username
varchar(32) NOT NULL COMMENT ‘用户名称‘,
birthday
datetime default NULL COMMENT ‘生日‘,
sex
char(1) default NULL COMMENT ‘性别‘,
address
varchar(256) default NULL COMMENT ‘地址‘,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
三、创建相关包和类
IUserDao接口
public interface IUserDao {
List<User> findAll();//获取所有用户信息
}
User类
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public User(String address) {
this.address = address;
}
public User(Integer id, String username, Date birthday, String sex, String address) {
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username=‘" + username + ‘\‘‘ +
", birthday=" + birthday +
", sex=‘" + sex + ‘\‘‘ +
", address=‘" + address + ‘\‘‘ +
‘}‘;
}
}
四、配置文件
<?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">
<!--mybatis主配置文件 -->
<configuration>
<!-- 配置环境 default属性是在默认情况下启用的数据源-->
<environments default="mysql">
<!-- 配置mysql的环境 id属性用来唯一标识数据源
输入<environment>标签 会自动生成 transactionManager dataSource
-->
<environment id="mysql">
<!-- 配置事务类型
type可以有三个值:JDBC,MANAGED ,自定义
JDBC:采用JDBC管理事务
MANAGED:代用容器方式管理事务
自定义:自定义事务事务管理方法
-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池)
type属性配置连接数据库的方式:
UNPOOLED 非连接池数据库
POOLED 连接池数据库
JNDI JNDI数据源
自定义数据源
-->
<dataSource type="POOLED">
<!-- 配置数据库的基本信息-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy?&useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件
如果使用xml配置,不用注解配置的话,mapper中的属性resource的值就指明dao中实现类的配置文件
如果使用注解配置,resource的值只需改为全类名,并在要装配的类中的方法加注解@Select("sql语句")
-->
<mappers>
<mapper resource="com/it/dao/IUserDao.xml"/>
</mappers>
</configuration>
IUserDao.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">
<!-- 如果是用注解配置,这个xml文件就不需要了,在对应接口的方法前面加@Select注释就行了 -->
<!-- 这个地方,通过namespace和select中的id属性可以唯一确定一个方法 -->
<mapper namespace="com.itcast.dao.IUserDao">
<!-- 配置查询所有的SQL语句-->
<!-- id 写指定的方法名
resultType属性为你要将查询结果注入到那个类,值写全类名
-->
<select id="findAll" resultType="com.itcast.domain.User">
<!-- 写sql语句,分号写不写无所谓 -->
select * from user
</select>
</mapper>
log4j.properties配置
如果不用日志的话完全不用配置
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
测试类MyTest
public class MybatisTest {
public static void main(String[] args) throws IOException {
//读取配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//建造者模式,builder就是建造者
SqlSessionFactory factory = builder.build(in); //工厂模式
//使用工厂生产SqlSession对象
SqlSession session = factory.openSession();
//使用SqlSession创建Dao接口的代理对象
IUserDao userDao = session.getMapper(IUserDao.class);//创建Dao接口实现类使用了代理模式
//使用代理对象执行方法
List<User> users = userDao.findAll();
for (User user: users){
System.out.println(user);
}
//释放资源
session.close();
in.close();
}
}
标签:domain ble tty efault category you Opens 生成 rop
原文地址:https://www.cnblogs.com/tyhA-nobody/p/13196014.html