Mybatis环境搭建
1.创建一个空的maven工程,导入jar包
我们使用的开发工具为idea2018
成功后我们应该得到这样的目录结构
我们在pom.xml中配置我们所需的jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.daixin.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- mybatis依赖坐标 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mysql 依赖坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 单元测试坐标依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2.在数据库中建表
CREATE TABLE USER (
Id INT(11) PRIMARY KEY AUTO_INCREMENT,
UserName VARCHAR(18) DEFAULT NULL,
Age INT(11) DEFAULT NULL
)user
别忘记添加一些可供我们查询的数据
3.创建Dao层以及接口
package com.daixin.mybatisdemo.domain;
public class UserDao {
private Integer Id;//注意变量和sql表中相同
private String UserName;
private Integer Age;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public Integer getAge() {
return Age;
}
public void setAge(Integer age) {
Age = age;
}
@Override
public String toString() {
return "UserDao{" +
"Id=" + Id +
", UserName='" + UserName + '\'' +
", Age=" + Age +
'}';
}
}
package com.daixin.mybatisdemo.dao;
import domain.UserDao;
import java.util.List;
public interface ImUserDao {
/*
用户查询方法
*/
List<UserDao> findALL();
}
4.配置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">
<!--数据库配置的基本信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdemo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--指定映射文件配置 -->
<mappers>
<mapper resource="Mapper.xml"/>
</mappers>
</configuration>
<?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.daixin.mybatisdemo.dao.ImUserDao">
<select id="findALL" resultType="com.daixin.mybatisdemo.domain.UserDao">
select * from user
</select>
</mapper>
测试
package com.daixin.test;
import com.daixin.mybatisdemo.dao.ImUserDao;
import com.daixin.mybatisdemo.domain.UserDao;
import com.sun.deploy.model.Resource;
import jdk.internal.util.xml.impl.Input;
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 Test {
public static void main(String[] args) throws Exception{
//1.读取配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工厂生产SqlSession对象
SqlSession sqlSession = factory.openSession();
//4.使用SqlSession工厂创建Dao接口的代理对象
ImUserDao userDao =sqlSession.getMapper(ImUserDao.class);
//5.使用代理对象执行方法
List<UserDao> users =userDao.findALL();
for (UserDao user:users
) {
System.out.println(user);
}
//6.释放资源
sqlSession.close();
in.close();
}
}
我就们成功了查询到了数据库里的信息