这篇文章主要介绍mysql连接数据库并测试的方式方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1.通过maven导入关于mybatis的jar包
可以通过阿里云的maven库直接下载相关jar包
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>MySQL</groupId>
<artifactId>mysql-connector-Java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
2.编写mybatis的配置文件
<?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/my_schema?useSSL=true" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="UserMapper.xml" />
</mappers>
</configuration>
3.编写映射文件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="userMapper">
<select id="selectUser" resultType="entity.User">
select * from user
</select>
</mapper>
4.写出实体类User,很平常的类,就不在过多解释.
public class User {
private String username;
private String email;
private String password;
private String create_time;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCreate_time() {
return create_time;
}
public void setCreate_time(String create_time) {
this.create_time = create_time;
}
@Override
public String toString() {
return "User [username=" + username + ", email=" + email + ", password=" + password + ", create_time="
+ create_time + "]";
}
public User(String username, String email, String password, String create_time) {
super();
this.username = username;
this.email = email;
this.password = password;
this.create_time = create_time;
}
public User() {
super();
}
}
5.在写一个类MybatisSamples,里面包含main方法,进行测试.
具体代码如下
public static void main(String[] args) {
String resource = "mybatis-config.xml";
SqlSession session = null;
try {
InputStream is = Resources.getResourceAsStream(resource);
//获取一个session工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//通过工厂获取session
session = sqlSessionFactory.openSession();
//根据映射文件中select标签的id获取集合
List<User> list = session.selectList("userMapper.selectUser");
for(User user: list) {
System.out.println(user);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//注意关闭session!!!
session.close();
}
}
执行结果如下:
附:
包结构
数据库结构
总结:想入门mybatis最重要的两步,第一是写配置文件,配置数据库和mapper,第二步是写好mapper和实体类,mapper中写sql语句,然后可以就可以通过mybatis把表中的数据映射成为一个对象了。
以上是“mysql连接数据库并测试的方式方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!