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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。