Mybatis入门案例
1.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">
<!--mybatis的主配置文件 -->
<configuration>
<!--配置环境-->
<environments default="mysql">
<!--配置mysql的环境-->
<environment id="mysql">
<!--配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据源(连接池)-->
<dataSource type="POOLED">
<!--配置连接数据库的4个基本信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
<mappers>
<mapper resource="com/itcast/dao/IUserDao.xml"/>
</mappers>
<!-- <mappers> <!–全限定类名–>
<mapper class="com.itcast.dao.IUserDao"/>
</mappers>-->
</configuration>
2.log4j.properties 配置文件
### 设置###
log4j.rootLogger = debug,stdout,D,E
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = C://Users//cs//Desktop/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = C://Users//cs//Desktop/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
3.User 类
package com.itcast.domin;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String 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 + '\'' +
'}';
}
}
4.IUserDao 接口
package com.itcast.dao;
import com.itcast.domin.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
*
* 用户持久层接口
*/
public interface IUserDao {
/**
* 查询所有操作
* @return
*/
/* @Select("select * from user")*/
List<User> findAll();
}
5.IUserDao配置文件
<?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">
<mapper namespace="com.itcast.dao.IUserDao">
<!--配置查询所有-->
<select id="findAll" resultType="com.itcast.domin.User">
select * from user
</select>
</mapper>
6.MybatisTest 测试类
package com.itcast.test;
import com.itcast.dao.IUserDao;
import com.itcast.domin.User;
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.InputStream;
import java.util.List;
public class MybatisTset {
/**
* 入门案例
*/
public static void main(String[] args) throws Exception {
//1.读取配置文件
InputStream in= Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSeesionFactory工厂
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
//3.使用工厂生产SqlSession对象
SqlSession session=factory.openSession();
//4.使用SqlSession 创建Dao接口的代理对象
IUserDao userDao=session.getMapper(IUserDao.class);
//5.使用代理对象执行方法
List<User> users=userDao.findAll();
for (User user:users){
System.out.println(user);
}
//6.释放资源
session.close();
in.close();
}
}
7.补充: 建立数据库
CREATE DATABASE eesy;
SELECT * FROM user
INSERT INTO user VALUES(41,'老王','2019-11-28 3:34:23','男','北京'),(42,'小二王','2019-11-26 13:56:27','男','北京金燕龙'),
(43,'老二王','2019-11-24 18:29:53','女','北京金燕龙'),(44,'西安交大','2019-11-21 23:44:13','男','北京金燕龙'),
(45,'老王','2019-11-28 3:34:23','男','北京'),(48,'小马宝莉','2019-11-28 22:33:13','男','北京修正');