壹.搭建环境
1.通过maven导入Jar包
<dependencies>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--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>8.0.27</version>
</dependency>
</dependencies>
2. 编写mybatis配置文件(建议置于resources文件夹下)
<?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"><!--mybatis获取连接的方式-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/><!--驱动-->
<!--useSSL 是否使用安全连接-->
<!--useUnicode 是否使用Unicode编码-->
<!--characterEncoding 传输数据的编码方式-->
<!--mysql8及以上的版本需要设置时区-->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="数据库用户名"/>
<property name="password" value="数据库密码"/>
</dataSource>
</environment>
</environments>
<!--注册mapper-->
<mappers>
<mapper resource="cn/alan/Mapper/UserMapper.xml"/>
</mappers>
</configuration>
3.编写mybatis工具类
//sqlsessionFactory --> sqlsession 通过sqlsession工厂获得sqlsession对象
public class mybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
//获取 sqlsessionFactory 对象
static {
try {
String resource = "mybatis-config.xml";//mybatis配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);//获取配置文件中的配置信息
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//通过配置信息创建sqlsession工厂
} catch (IOException e) {
e.printStackTrace();
}
}
//获得 sqlSession 实例
//sqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
贰.获取数据
1.编写实体类
//实体类
public class User {
private String Username;
private String Password;
private String Email;
private String Name;
private int Tel;
//Alt + Fn + ins(笔记本键盘)
//Alt + ins(外设键盘)
//Constructor 构造方法
public User() {
}
//Constructor 构造方法
//Ctrl进行多选
public User(String username, String password, String email, String name, int tel) {
Username = username;
Password = password;
Email = email;
Name = name;
Tel = tel;
}
//Getter and Setter
public String getUsername() {
return Username;
}
public void setUsername(String username) {
Username = username;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getTel() {
return Tel;
}
public void setTel(int tel) {
Tel = tel;
}
//重写toString
@Override
public String toString() {
return "Username="+Username+","+"Password="+Password+","+"Email="+Email+","+"Name="+Name+","+"Tel="+Tel;
}
}
2.编写Mapper接口
public interface UserMapper {
List<User> getUserList();
}
3.编写接口实现类(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">
<!--namespace绑定一个对应的Mapper接口-->
<mapper namespace="cn.alan.Mapper.UserMapper">
<!--实现接口 id为接口内的方法 resultType为返回值的类型-->
<select id="getUserList" resultType="cn.alan.POJO.User">
<!--在select标签内重写getUseList方法-->
select * from mybatis.user;
</select>
</mapper>
叁.项目测试
编写测试用类
public class UserMapperTest {
@Test
public void getUserList(){
//获得sqlsession对象
SqlSession sqlSession = mybatisUtils.getSqlSession();
try{
//执行sql
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
//User user = new User ("nan",-1,-1);
//将返回数据分行输出(若不循环输出 所有数据会在同一行输出)
for (User user : userList){
System.out.println(user);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
//关闭sqlsession
sqlSession.close();
}
}
}
运行效果
项目结构
可能遇到的问题:资源导出失败
<!--当程序找不到xml配置文件或properties配置文件时可手动为其指明位置-->
<!--写入pom.xml文件中-->
<build>
<resources>
<resource>
<directory>main/Java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>