本文主要和大家分享mysql連接資料庫並測試實例,希望能幫助大家。
1.透過maven導入關於mybatis的jar包
#可以直接下載相關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的設定檔
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
##" <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把表中的資料映射成為一個物件了。 ######相關推薦:#########php mysql連接資料庫實例_MySQL############MySQL連接資料庫函數記憶體外洩解決方案##### #######python和mysql連接資料庫的問題#######
以上是mysql連接資料庫並測試實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!