您好,登錄后才能下訂單哦!
這篇文章主要介紹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連接數據庫并測試的方式方法”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。