您好,登錄后才能下訂單哦!
1.把所需jar(mysql-connector-java-5.1.18-bin.jar和mybatis-3.3.0.jar)拷貝到lib目錄下
2.編寫與數據庫對應的實體類UserInfo
3.創建mybatis-config.xml配置文件:
<!-- 配置數據庫連接 --> <environments default="developments"> <environment id="mysql"> <transactionManager type="JDBC"> <property name="" value=""/> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="wc654321"/> </dataSource> </environment> </environments> <!-- 對應的sql映射文件userinfo.xml --> <mappers> <mapper resource="com/wc/config/userinfo.xml"/> </mappers>
4.創建DBFactory類獲取SqlSessionFactory
/** * 訪問數據庫類 */ public class DBFactory { public static SqlSessionFactory sqlSessionFactory = null; static { try { String resource = "com/wc/config/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"mysql");//第二個參數:指定需要連接的數據庫配置 } catch (IOException e) { e.printStackTrace(); } } public static SqlSessionFactory getFactory(){ return sqlSessionFactory; }
或者
/** * 訪問數據庫類 */ public class DBAccess { public SqlSession getSqlSession() throws IOException{ //(1)通過配置文件獲取數據庫連接相關信息<br> Reader reader = Resources.getResourceAsReader("com/wc/config/Configuration.xml"); //(2)通過配置信息構建SqlSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader,"mysql"); //(3)通過SqlSessionFactory打開數據庫回話 SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }
5.創建mybatis的sql映射文件userinfo.xml
<mapper namespace="com.wc.Interface.IuserInfo"> :對應接口,統一命名空間,重要!
resultMap: property對應實體類屬性,column 對應著數據庫字段
sql語句:
id="selectUserByInterface" 唯一標識
parameterType="java.lang.Integer" 參數的類型
resultMap="userResult" 返回類型
select <include refid="columns"/> from user_info order by user_id
<sql id="columns">user_id,user_name,user_sex</sql>
<?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="com.wc.Interface.IUserInfo"> <resultMap type="com.wc.po.UserInfo" id="UserResult"> <id column="user_id" jdbcType="INTEGER" property="userId"/> <result column="user_name" jdbcType="VARCHAR" property="userName"/> <result column="user_sex" jdbcType="VARCHAR" property="userSex"/> </resultMap> <select id="selectUserByInterface" parameterType="com.wc.po.UserInfo" resultMap="UserResult"> select <include refid="columns"/> from user_info order by user_id </select> <select id="selectOneUserByInterface" parameterType="java.lang.Integer" resultMap="UserResult"> select <include refid="columns"/> from user_info where user_id=#{userID} </select> </mapper>
6.通過接口的方式調用XML的查詢語句(面向接口式編程)
public interface IUserInfo { /** * 根據user_id查詢單個信息 */ public UserInfo selectOneUserByInterface(int i); /** * 查詢全部信息 */ public List<UserInfo> selectUserByInterface(); }
7.從SqlSessionFactory里面獲取SqlSession:DBFactory.getFactory().openSession();
在從SqlSession拿到增刪修查一系列方法
/** * 使用接口的方式進行全部查詢 */ @Test public void selectUserByInterface() { List<UserInfo> userInfo = new ArrayList<UserInfo>(); // 獲取sqlSession SqlSession session = null; try { // 從SqlSessionFactory里獲取SqlSession session = DBFactory.getFactory().openSession(); //接口 IUserInfo iUserInfo = session.getMapper(IUserInfo.class); userInfo = iUserInfo.selectUserByInterface(); if (userInfo != null && userInfo.size() > 0) { for (UserInfo user : userInfo) { System.out.println(user); } } } catch (Exception e) { } finally { if (session != null) { session.close(); } } } /** * 使用接口的方式進行單條查詢 */ @Test public void selectOneUserByInterface() { // 獲取sqlSession SqlSession session = null; try { // 從SqlSessionFactory里獲取SqlSession session = DBFactory.getFactory().openSession(); //UserInfo userInfo = session.selectOne("userinfo.selectOneUser",3);userinfo是以前的命名空間 IUserInfo iUserInfo = session.getMapper(IUserInfo.class); UserInfo userInfo = iUserInfo.selectOneUserByInterface(3); System.out.println(userInfo); } catch (Exception e) { } finally { if (session != null) { session.close(); } } }
最后關閉SqlSession
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。