【MyBatis】三种开发方式
来源:互联网 发布:python hexdump 编辑:程序博客网 时间:2024/06/17 13:30
环境配置
1.创建普通Java项目;
2.创建 config 和 lib 文件夹;
3.config 文件夹添加到类目录下:选中项目-BuildPath-AddFolder
4.添加 jar包 到 lib文件夹,同时 AddBuildPath 令其出现在 Referenced Libraries 中
5.添加 log4j.xml 文件到 config文件夹
6.添加 resource.xml 文件到 config 文件夹
7.添加 dbconfig-properties 文件到 config文件夹
8.在 resource.xml 文件中写 dbconfig-properties 路径
开发步骤
1.在实体同级建立一个同名的xml文件,和实体类相对应;
2.在xml文件设置命名空间为实体类的全路径;
3.把dtd文件导入项目,Window-Preference-XML-XMLCatalog-Add(设置提示);
4.编写SQL语句,写id,parameterType,resultType;
5.编写测试类,resource-reader-SQLSessionFactory-SqlSession;
三种开发方式
方式一:配置文件
resource.xml
<mappers> <mapper resource="mappers/EmployeeMapper.xml" /></mappers>
EmployeeMapper.xml
<mapper namespace="com.dao.bean.Employee"> <select id="selOne" resultType="com.bean.Employee"> select id, name e_name, gender, address from employee where id = #{id} </select></mapper>
方式二:接口
resource.xml
<mappers> <mapper class="com.dao.EmployeeDAO2"</mappers>
EmployeeDAO2.java
public interface EmployeeDAO2{ @Select("select * from employee") public List<Employee> getAll(); public Employee selOne(String id);}
MyBatisTest.java
public class MyBatisTest{ @Test public void test() throws IOException{ //创建一个sqlSession Reader reader = Resources.getResourceAsReader("resource.xml"); //创建SQLSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //true表示自动提交,否则需要使用commit提交,默认为false SqlSession session = sqlSessionFactory.openSession(true); // 创建接口对象,这是session对象通过动态代理自动为我们创建 EmployeeDAO2 empDao = session.getMapper(EmployeeDAO2.class); //使用创建好的接口对象调用方法 List<Employee> empList = empDao.getAll(); }}
方式三:接口+配置文件
resource.xml
<mappers> <mapper resource="mappers/EmployeeMapper.xml" /></mappers>
EmployeeMapper.xml
<mapper namespace="com.dao.EmployeeDAO"> <select id="selAll" resultType="com.bean.Employee"> select * from employee </select> <select id="selOne" resultType="com.bean.Employee"> select id, name e_name, gender, address from employee where id = #{id} </select></mapper>
EmployeeDAO.java
public interface EmployeeDAO{ public List<Employee> selAll(); public Employee selOne(String id);}
MyBatisTest.java
public class MyBatisTest{ @Test public void test() throws IOException{ //创建一个sqlSession Reader reader = Resources.getResourceAsReader("resource.xml"); //创建SQLSessionFactory SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); //true表示自动提交,否则需要使用commit提交,默认为false SqlSession session = sqlSessionFactory.openSession(true); // 创建接口对象,这是session对象通过动态代理自动为我们创建 EmployeeDAO empDao = session.getMapper(EmployeeDAO.class); //使用创建好的接口对象调用方法 Employee emp = empDao.selOne("2"); List<Employee> empList = empDao.selAll(); }}
阅读全文
0 0
- 【MyBatis】三种开发方式
- Mybatis两种开发方式
- spring集成mybatis的三种方式
- spring mybatis 整合的三种方式
- Mybatis Spring 组合的三种方式
- Spring+Mybatis 三种查询映射方式
- Spring整合Mybatis的三种方式
- mybatis一对一的三种实现方式
- mybatis - 条件查询的三种方式
- mybatis 传递参数的三种方式
- MyBatis模糊查询的三种方式
- springboot整合mybatis的三种方式
- Mybatis入门开发之dao方式(三)
- MyBatis开发dao的两种方式
- Servlet开发三种方式
- MyBatis学习(三)--Mybatis编写方式
- spring 与mybatis的三种整合方式
- MyBatis模糊查询的三种拼接方式
- FDD and HDD
- 鼠标拖拽功能实现
- 2.2多线程(多线程间是如何进行通讯的)
- jdk中的设计模式
- jQuery学习二-添加删除元素
- 【MyBatis】三种开发方式
- centos 6.5下使用中文输入法,切换图形界面和命令行
- 递归求解数组中的最大值
- 2.3hashmap的存储结构
- @SelectProvider,@Select和xml用法的一点理解
- java使用poi读取存储excel表格,包括xls和xlsx格式
- 2.4ArrayList和LinkedList的区别及详细原因
- imageLoader代码使用
- 分饼干