mysql連接數(shù)據(jù)庫(kù)并測(cè)試案例分享
發(fā)表時(shí)間:2023-07-14 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]本文主要和大家分享mysql連接數(shù)據(jù)庫(kù)并測(cè)試實(shí)例,希望能幫助到大家。1.通過(guò)maven導(dǎo)入關(guān)于mybatis的jar包可以通過(guò)阿里云的maven庫(kù)直接下載相關(guān)jar包<dependencies...
本文主要和大家分享mysql連接數(shù)據(jù)庫(kù)并測(cè)試實(shí)例,希望能幫助到大家。
1.通過(guò)maven導(dǎo)入關(guān)于mybatis的jar包
可以通過(guò)阿里云的maven庫(kù)直接下載相關(guān)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" />
<!-- 配置數(shù)據(jù)庫(kù) -->
<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.寫出實(shí)體類User,很平常的類,就不在過(guò)多解釋.
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.在寫一個(gè)類MybatisSamples,里面包含main方法,進(jìn)行測(cè)試.
具體代碼如下
public static void main(String[] args) {
String resource = "mybatis-config.xml";
SqlSession session = null;
try {
InputStream is = Resources.getResourceAsStream(resource);
//獲取一個(gè)session工廠
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//通過(guò)工廠獲取session
session = sqlSessionFactory.openSession();
//根據(jù)映射文件中select標(biāo)簽的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 {
//注意關(guān)閉session!!!
session.close();
}
}
執(zhí)行結(jié)果如下:
附:
包結(jié)構(gòu)
數(shù)據(jù)庫(kù)結(jié)構(gòu)
總結(jié):想入門mybatis最重要的兩步,第一是寫配置文件,配置數(shù)據(jù)庫(kù)和mapper,第二步是寫好mapper和實(shí)體類,mapper中寫sql語(yǔ)句,然后可以就可以通過(guò)mybatis把表中的數(shù)據(jù)映射成為一個(gè)對(duì)象了。
相關(guān)推薦:
php mysql連接數(shù)據(jù)庫(kù)實(shí)例_MySQL
MySQL連接數(shù)據(jù)庫(kù)函數(shù)內(nèi)存泄露解決方案
python和mysql連接數(shù)據(jù)庫(kù)的問(wèn)題
以上就是mysql連接數(shù)據(jù)庫(kù)并測(cè)試實(shí)例分享的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。