mybatis简介
mybatis主要封装了Java对数据库的操作,是一款优秀的持久层框架,它免除了Javajdbc带来的复杂处理程序。
maven初始mybatis项目
在pom.xml文件中导入
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
在maven中创建相应目录 首先创建的数据是mybatis,数据表是user。整型的id,两个String类型的namg和pwd
首先创建mybatis-config.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>
<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="123456789"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/yc/dao/UserMapper.xml"/>
</mappers>
</configuration>
创建mybatisutils工具类。
package com.yc.utils;
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.InputStream;
public class mybatisutils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
创建User用户类
package com.yc.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
创建Userdao接口。
package com.yc.dao;
import com.yc.pojo.User;
import java.util.List;
public interface Userdao {
List<User> getUserList();
}
创建UserMapper.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">
<mapper namespace="com.yc.dao.Userdao">
<select id="getUserList" resultType="com.yc.pojo.User">
select * from mybatis.user
</select>
</mapper>
创建测试类test
package com.yc.dao;
import com.yc.pojo.User;
import com.yc.utils.mybatisutils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class test {
@Test
public void test(){
SqlSession sqlSession= mybatisutils.getSqlSession();
Userdao user=sqlSession.getMapper(Userdao.class);
List<User> list=user.getUserList();
for(User u: list) {
System.out.println(u);
}
sqlSession.close();
}
}
最终输出是发现找不到UserMapper.xml文件,因为文件没有放在配置目录下,需要导入maven资源过滤。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
最终输出
User{id=1, name=‘java’, pwd=‘56989’} User{id=2, name=‘python’, pwd=‘2122’} User{id=3, name=‘PHP’, pwd=‘5555’}
|