ibatis

Mybatis Util包

拟墨画扇 提交于 2019-12-04 09:16:18
package util; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MyBatisUtil { private static SqlSessionFactory factory; static{//在静态代码块下,factory只会被创建一次 System.out.println("static factory==============="); try { InputStream is = Resources.getResourceAsStream("conf.xml"); factory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { // TODO Auto-generated catch block e

Calling Oracle stored procedures with MyBatis

五迷三道 提交于 2019-12-04 07:47:17
I am in the process of moving our database over to Oracle from SQL Server 2008 but cannot get MyBatis to work. Given the following example: UserMapper.xml (example) <resultMap type="User" id="UserResult"> <id property="userId" column="userId"/> <result property="firstName" column="firstName"/> <result property="lastName" column="lastName"/> </resultMap> <select id="getUsers" statementType="CALLABLE" resultMap="UserResult"> {CALL GetUsers()} </select> UserDAO.java public interface UserDAO { public List<User> getUsers(); } SQL Server procedure CREATE PROCEDURE [dbo].[GetUsers] AS BEGIN SET

iBatis SqlMap的配备总结

孤街浪徒 提交于 2019-12-04 06:58:19
iBatis SqlMap的配置总结 核心提示:SqlMap的配置是iBatis中应用的核心。这部分任务占据了iBatis开发的70的工作量。 1、命名空间: sqlMap namespace=Account,在此空间外要引用此空间的元素,则需要加上命名空间名。 2、实体的别名: typeAlias alias=Account type=com.lavasoft.ibatissut.sim SqlMap的配置是iBatis中应用的核心。这部分任务占据了iBatis开发的70的工作量。 1、命名空间: <sqlMap namespace="Account">,在此空间外要引用此空间的元素,则需要加上命名空间名。 2、实体的别名: <typeAlias alias="Account" type="com.lavasoft.ibatissut.simple.domain.entity.Account"/> 如果有用到的全名的地方,可以用别名代替,受命名空间约束。 3、插入操作 对于自增主键的表,插入可以不配置插入的主键列。否则是必须的。 4、获取主键 插入语句之前配置:主要是针对Sequence主键而言,插入前必须指定一个主键值给要插入的记录。Oracle、DB2亦如此,方法是在插入语句标签<insert....>之前配置上: <insert id="insertAccount"

ibatis sqlMap 使用

爷,独闯天下 提交于 2019-12-04 06:58:05
SqlMap的配置是iBatis中应用的核心。这部分任务占据了iBatis开发的70的工作量。 1、命名空间: <sqlMap namespace="Account"> 在此空间外要引用此空间的元素,则需要加上命名空间名。 2、实体的别名: <typeAlias alias="Account" type="com.lavasoft.ibatissut.simple.domain.entity.Account"/> 如果有用到的全名的地方,可以用别名代替,受命名空间约束。 3、插入操作 对于自增主键的表,插入可以不配置插入的主键列。否则是必须的。 4、获取主键 插入语句之前配置:主要是针对Sequence主键而言,插入前必须指定一个主键值给要插入的记录。Oracle、DB2亦如此,方法是在插入语句标签<insert....>之前配置上: <insert id="insertAccount" parameterClass="Account"> <selectKey resultClass="long" keyProperty="sctId"> SELECT SEQ_TEST.NEXTVAL FROM DUAL </selectKey> insert into .... ........ </insert> 插入语句之后配置:主要是针对自增主键的表而言,这类表在插入时不需要主键

[springboot 开发单体web shop] 5. 用户登录及首页展示

荒凉一梦 提交于 2019-12-04 06:46:05
用户登录及前端展示 用户登录 在之前的文章中我们实现了用户注册和验证功能,接下来我们继续实现它的登录,以及登录成功之后要在页面上显示的信息。 接下来,我们来编写代码。 实现service 在 com.liferunner.service.IUserService 接口中添加用户登录方法: public interface IUserService { ... /** * 用户登录 * @param userRequestDTO 请求dto * @return 登录用户信息 * @throws Exception */ Users userLogin(UserRequestDTO userRequestDTO) throws Exception; } 然后,在 com.liferunner.service.impl.UserServiceImpl 实现类中实现: @Service @Slf4j public class UserServiceImpl implements IUserService { ... @Override public Users userLogin(UserRequestDTO userRequestDTO) throws Exception { log.info("======用户登录请求:{}", userRequestDTO); Example

Mybatis 源码(三)Mybatis 代理模块

匆匆过客 提交于 2019-12-04 06:02:58
在使用Mybatis的时候大家可能都有一个疑问,为什么只写Mapper接口就能操作数据库? 它的主要实现思想是:使用动态代理生成实现类,然后配合xml的映射文件中的SQL语句来实现对数据库的访问。 Mybatis编程模型 Mybatis是在iBatis上演变而来ORM框架,所以Mybatis最终会将代码转换成iBatis编程模型,而 Mybatis 代理阶段主要是将面向接口编程模型,通过动态代理转换成ibatis编程模型。 我们不直接使用iBatis编程模型的原因是为了解耦,从下面的两种示例我们可以看出,iBatis编程模型和配置文件耦合很严重。 面向接口编程模型 @Test // 面向接口编程模型 public void quickStart() throws Exception { // 2.获取sqlSession try (SqlSession sqlSession = sqlSessionFactory.openSession()) { initH2dbMybatis(sqlSession); // 3.获取对应mapper PersonMapper mapper = sqlSession.getMapper(PersonMapper.class); JdkProxySourceClassUtil.writeClassToDisk(mapper.getClass()

How do you write arrays to an Oracle 10g XE db using iBatis?

末鹿安然 提交于 2019-12-04 05:33:49
问题 I have looked for the answer to this high and low but cannot get the answer. Basically I have an object I am writing to my db using iBatis. This works fine with primitive types like strings, int's etc but one of the attributes of my object is an array of other objects. I would like to be able to persist this and then later call the 'selectById' statement and retrieve the full object including the array. Here is the code I have so far: Mapper.xml <insert id="insertTrade" parameterClass=

Can MyBatis create the database schema?

给你一囗甜甜゛ 提交于 2019-12-04 05:13:59
Has MyBatis any feature to allow create the SQL schema from de Class Model like Hibernate does? I'm looking for that in Google and I only found information about MyBatis Generator ( http://mybatis.github.io/generator/ ). This tool seems to be useful for generate the Java model from the SQL Schema, which is just the opposite I want. Can MyBatis create the database schema? I'm afraid not. In order to do that you need an ORM and MyBatis is not an ORM. With an ORM solution (like Hibernate for example) you map tables to entities. The entity is the (object) representation of the (relational) table

How to manage read-only DB connections at an application level

别来无恙 提交于 2019-12-03 20:53:41
We are using Java/Spring/Ibatis/MySql. Is there a way with these technologies to manage read-only connections at an application level. I am looking to add an extra layer of safeguarding on top of having read-only MySql users. It would be nice if BasicDataSource or SqlMapClientTemplate provided configuration for read-only connections. Otherwise, it seems like I'm left to only MySql read users and enforcing interfaces with only read methods. Thanks for example Connection#CreateStatement can takes parameters statement = connection.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet

数据库连接池应用中数据库服务器断开超时连接的问题

六月ゝ 毕业季﹏ 提交于 2019-12-03 20:23:09
数据库应用开发过程中,我们可能会遇到一个问题:应用使用了数据库连接池,每经过指定时间后,发出到数据库服务器的任何请求都会失败,而且有且仅有一次失败,之后的正常访问都没有问题。尤其是在Web应用中,如果晚上时段没有访问,而第二天第一个访客的经历就是碰到一个数据库访问错误,如果开发系统的程序员没有注意这个问题的话,可能终端用户访问会看到抛出的一堆数据库异常信息。 其实,这个问题的主要原因是,应用中数据库连接池中会保存指定数量的数据库连接实例,而这些连接实例并没有定时地检测其到数据库服务器连接是否正常;数据库服务器可以配置一个数据库连接实例的超时时间,超过时间后它会自动断开连接。也就是,被断开的那个连接此时仍然保存在应用的数据库连接池内,下次被使用的时候就会发生数据库连接断开而导致一次访问失败。 解决上述连接关闭的方案有两种值得推荐: 如果能够提供这样一种检测机制,在应用的连接池管理中定时地检测连接池中连接的有效性,就完全可以避免上面描述的问题。 在应用代码中通过异常处理机制,来实现该次业务的重新处理,也可以很好地避免。 我们举一个例子,使用Java开发的Web系统,Tomcat作为HTTP服务器,MySQL作为数据库,抛出异常的信息如下所示: [http-bio-8080-exec-10] 2012-11-28 00:55:43 [org.shirdrn.wm.de.action