- 浏览: 1152462 次
- 性别:
- 来自: 火星郊区
博客专栏
-
OSGi
浏览量:0
文章分类
- 全部博客 (695)
- 项目管理 (48)
- OSGi (122)
- java (79)
- Vaadin (5)
- RAP (47)
- mysql (40)
- Maven (22)
- SVN (8)
- 孔雀鱼 (10)
- hibernate (9)
- spring (10)
- css (3)
- 年审 (6)
- ant (1)
- jdbc (3)
- FusionCharts (2)
- struts (4)
- 决策分析 (2)
- 生活 (10)
- 架构设计 (5)
- 破解 (2)
- 狼文化 (4)
- JVM (14)
- J2EE (1)
- 应用服务器 (1)
- 我的链接 (5)
- 数学 (2)
- 报表 (1)
- 百科 (6)
- Flex (7)
- log4j (2)
- PHP (1)
- 系统 (2)
- Web前端 (7)
- linux (6)
- Office (1)
- 安全管理 (5)
- python (2)
- dom4j (1)
- 工作流 (3)
- 养生保健 (4)
- Eclipse (8)
- 监控开发 (1)
- 设计 (3)
- CAS (1)
- ZK (41)
- BluePrint (3)
- 工具 (1)
- SWT (7)
- google (2)
- NIO (1)
- 企业文化 (2)
- Windoes (0)
- RCP (7)
- JavaScript (10)
- UML (1)
- 产品经理 (2)
- Velocity (10)
- C (1)
- 单元测试 (1)
- 设计模式 (2)
- 系统分析师 (2)
- 架构 (4)
- 面试 (2)
- 代码走查 (1)
- MongoDB (1)
- 企业流程优化 (1)
- 模式 (1)
- EJB (1)
- Jetty (1)
- Git (13)
- IPV6 (1)
- JQuery (8)
- SSH (1)
- mybatis (10)
- SiteMesh (2)
- JSTL (1)
- veloctiy (1)
- Spring MVC (1)
- struts2 (3)
- Servlet (1)
- 权限管理 (1)
- Java Mina (1)
- java 系统信息 (6)
- OSGi 基础 (3)
- html (1)
- spring--security (6)
- HTML5 (1)
- java爬虫搜索 (1)
- mvc (3)
最新评论
-
Tom.X:
http://osgia.com/
将web容器置于OSGi框架下进行web应用的开发 -
chenyuguxing:
你好, 为什么我的bundle export到felix工程中 ...
在Apache Felix中运行bundle -
string2020:
<niceManifest>true</ni ...
Bundle Plugin for Maven -
jsonmong:
OSGI,是未来的主流,目前已相当成熟。应用OSGI比较好的, ...
基于OSGi的声明式服务 -
zyhui98:
貌似是翻译过来的,有很少人在linux上做开发吧
如何成为“10倍效率”开发者
sqlSession.getMapper(UserMapper.class)的使用方式
- 博客分类:
- mybatis
说明:
1,本文采用的Mybatis框架版本为: mybatis-3.0.4.jar
2, 以持久化一个简单的User bean为例
User实体类的代码如下:
package bean; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User(String name, Integer age) { super(); this.name = name; this.age = age; } public User() { super(); } //必须要有这个无参构造方法,不然根据UserMapper.xml中的配置,在查询数据库时,将不能反射构造出User实例 }
User实体的映射器取名为UserMapper(接口),其代码如下
package Mapper; import bean.User; public interface UserMapper { public void insertUser(User user); public User getUser(String name); }
注意,该接口类似于Hibernate中的DAO层接口,唯一不同的是,MyBatis中,只声明该接口就可以,不需要去实现.
UserMapper映射器对应的配置文件如下(相当于Hibernate中的User.hbm.xml,当然这里面不是属性映射字段,而是直接是映射器中各种方法对应的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="Mapper.UserMapper"> <!-- 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”--> <insert id="insertUser" parameterType="User"> insert into vincent_user(name,age) values(#{name},#{age}) <!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 --> </insert> <!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候也要报错 --> <select id="getUser" resultType="User" parameterType="java.lang.String"> select * from vincent_user where name=#{name} </select> </mapper>
MyBatis的配置文件这里取名为mybatis-config.xml其配置代码如下(相当与Hibernate的hibernate.cfg.xml):
<?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> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> <typeAliases> <typeAlias alias="User" type="bean.User"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="jdbc"/> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@192.168.56.201:1521:system"/> <property name="username" value="vincent"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="resource/UserMapper.xml" /> </mappers> </configuration>
加载配置文件得到sqlSessionFactory的工具类
package Util; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private final static SqlSessionFactory sqlSessionFactory; static { String resource = "resource/mybatis-config.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { System.out.println(e.getMessage()); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }
测试代码如下:
package TestMapper; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; public class TestMapper { static SqlSessionFactory sqlSessionFactory = null; static { sqlSessionFactory = MyBatisUtil.getSqlSessionFactory(); } @Test public void testAdd() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User("tom",new Integer(5)); userMapper.insertUser(user); sqlSession.commit();//这里一定要提交,不然数据进不去数据库中 } finally { sqlSession.close(); } } @Test public void getUser() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUser("jun"); System.out.println("name: "+user.getName()+"|age: "+user.getAge()); } finally { sqlSession.close(); } } }
发表评论
-
Abator自动生成ibatis的相关配置和类
2012-11-14 16:02 1223一、abator自动生成配置: abator自动生成ibat ... -
MyBatis的关于批量数据操作的体会
2012-09-06 08:53 1074MyBatis的前身就是著名的Ibatis,不知何故脱 ... -
Mybatis目录
2012-09-06 08:53 2073Mybatis使用篇之一:Mybatis介绍 ... -
Mybatis使用篇之十二:实现物理分页
2012-09-06 08:53 1457Mybatis的自带分页方法只是逻辑分页,如果数据量很大,内存 ... -
Mybatis使用篇之七:存储过程调用
2012-09-05 11:27 1325如何使用Mybaits 调用数据库中的存储过程,下面以Ora ... -
Mybatis使用篇之四:整合Spring
2012-09-05 11:26 1781Mybaits为什么要整合Spring? 说白了其实 ... -
Mybatis使用篇之二:HelloWorld
2012-09-05 11:23 17131. 搭建开发环境 (1)新建一个Web工程,名称为 ... -
Mybatis使用篇之一:Mybatis介绍
2012-09-05 11:21 1890MyBatis是一个数据持久层框架,MyBatis 消除了 ... -
mybatis xml 一对多
2012-09-05 11:20 1328<? xml version = &q ...
相关推荐
UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteById(id); ``` 3. 提交事务:在MyBatis中,所有操作默认都是在事务中进行的。如果需要保存更改,必须调用`SqlSession`的`commit()`方法...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); Weekend<User> weekend = Weekend.of(User.class); weekend.weekendCriteria() .andIsNull(User::getId) .andBetween(User::getId,0,10) .andIn...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.selectAll(); return userList; } finally { sqlSession.close(); } } @Repository public int ...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class); // CRUD示例 User user = new User(); user.setUsername("testUser"); ...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<Order> orders = userMapper.selectUserOrders(userId); sqlSession.close(); ``` 这里,`sqlSessionFactory`是一个SqlSessionFactory对象...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 执行SQL List<User> users = userMapper.selectAll(); ``` 通过以上步骤,我们可以利用Mybatis的Mapper代理模式轻松地实现对数据库的操作。...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectAllUsers(); ``` 6. **日志信息表的CRUD操作**:对于日志信息表,我们需要实现增、删、改、查四个基本...
UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); sqlSession.close(); } ``` 三、注解开发的优势 1. 简洁性:使用注解可以减少...
UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.selectAllUsers(); sqlSession.close(); ``` 7. **测试**:编写JUnit测试用例,验证查询所有用户的功能是否正常工作...
UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.selectUser(1L); ``` MyBatis还支持动态SQL,这使得在XML映射文件中可以编写条件语句,比如IF、WHERE、choose(when/otherwise)...
UserMapper mapper = sqlSession.getMapper(UserMapper.class); int result = mapper.deleteById(1L); // 假设1L是User的主键ID ``` 这里的`result`返回的是受影响的行数,如果删除成功,返回1,否则返回0。 其次...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectAllUsers(); sqlSession.close(); ``` **总结** Ibatis作为一款轻量级的ORM框架,提供了灵活的SQL编写...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); ``` 2. **执行接口方法**: ```java List<User> users = userMapper.selectAllUsers(); ``` #### 4. 执行mapper接口方法 当获取到`...
UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = new User(1, "Jerry", 13); mapper.updateUser(user); sqlSession.commit(); } finally { sqlSession.close(); } ``` #### 五、...
UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); ``` Mybatis 还支持动态 SQL,使得 SQL 语句可以根据条件进行拼接。比如,我们可以使用 `<if>`、`<choose>`、`...
例如,`UserMapper userMapper = sqlSession.getMapper(UserMapper.class);` 6. **动态SQL**:Mybatis的mapper.xml文件支持动态SQL,如if、choose、when、otherwise等标签,可以根据条件动态生成SQL语句,增加了SQL...
UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); sqlSession.close(); } ``` 对于其他的CRUD操作,如`insert`、`update`和`...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectAllUsers(); for (User user : users) { System.out.println(user.getName()); } sqlSession.close()...
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectAllUsers(); for (User user : users) { System.out.println(user); } } finally { sqlSession....
UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.selectAllUsers(); ``` #### 二十、应用目录结构 - **目录结构**:推荐的应用程序目录结构应包括必要的配置文件和资源...