dao

02-基于注解的入门案例

痞子三分冷 提交于 2019-11-28 11:09:37
目录 一、方法 一、方法 把 IUserDao.xml 移除,在dao接口的方法上使用@Select注解,并且指定SQL语句。 /** * 查询所有 * @return */ @Select("select *from user") List<User> findAll(); 同时需要把SqlMapConfig.xml中的mapper配置时,使用class属性指定dao接口的全限定类名。 <?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> <environments default="mysql"> <!--配置mysql环境--> <environment id="mysql"> <!--配置事务类型--> <transactionManager type="JDBC"></transactionManager> <!--配置数据源,也叫连接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj

03-编写dao实现类方式

风格不统一 提交于 2019-11-28 11:09:32
目录 一、实现类 1.代码 2.解释 3.注意 一、实现类 1.代码 package dao.impl; import dao.IUserDao; import domain.User; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; public class UserDaoImpl implements IUserDao { private SqlSessionFactory factory; public UserDaoImpl(SqlSessionFactory factory){ this.factory=factory; } public List<User> findAll() { //1.使用工厂创建 SqlSession 对象 SqlSession session=factory.openSession(); //2.使用session执行查询所有的方法 List<User> users=session.selectList("dao.IUserDao.findAll"); //3.关闭session session.close(); //4.返回查询结果 return

01入门

左心房为你撑大大i 提交于 2019-11-28 11:08:10
目录 一、mybatis的入门 1.mybatis的环境搭建 Ⅰ maven的pom.xml配制 Ⅱ 创建实体类和dao的接口 Ⅲ 创建MyBatis的配置文件 Ⅳ 创建映射配置文件 2.环境搭建的注意事项 二、测试 1.配置log4j文件 2.创建测试类 Ⅰ 步骤 Ⅱ 代码 3.注意细节 4.注意事项 一、mybatis的入门 1.mybatis的环境搭建 创建 maven工程并导入坐标 创建实体类和dao的接口 创建MyBatis的只配置文件 SqlMapConfig.xml 创建映射配置文件 IUserDao.xml Ⅰ maven的pom.xml配制 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.MyBatis</groupId>

spring3-annotation-JdbcDaoSupport

末鹿安然 提交于 2019-11-28 10:34:58
Use annotation in dao @Repository("testDao") public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{ @Override public Object addObject(String sqlid, Object obj) { // TODO Auto-generated method stub return null; } Caused by: java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required I do not want to use : <bean id="termsDao" class="com.manage.base.dao.impl.TestDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> this code set in xml, and “jdbcTemplate” has been defined in other “spring-xml”。 How to solve this problem by an annotation :“'dataSource

MyBatis初探二

人走茶凉 提交于 2019-11-28 09:55:22
一、Mybatis 的 Dao 层两种开发方式 1、原始Dao开发模式( 自己手动实现Dao层接口和实现类 ) 》 实现思路:编写Dao接口和实现类,在Dao接口中注入单例的SqlSessionFactory,通过 SqlSessionFactory获取SqlSession来获取数据库操作对象。 》 package com.ts.dao; import com.ts.domain.Users; public interface UsersDao { //根据ID查询用户信息 Users findUserById(int id) throws Exception; } package com.ts.dao.impl; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.ts.dao.UsersDao; import com.ts.domain.Users; public class UsersDaoImpl implements UsersDao { private SqlSessionFactory sqlSessionFactory; //构造函数注入SqlSessionFactory public

第二节 Mapper代理开发模式

不打扰是莪最后的温柔 提交于 2019-11-28 09:00:23
一:原始的开发dao   实际开发中,我们程序员需要写dao接口和dao的实现类。    1.1 dao接口  1.2 dao实现类    1.3 测试类    总结:      在dao接口实现类方法存在很大大量重复的方法,这样会增加程序员的代码量     调用 sqlsession 方法时将 statement 的 id 硬编码 了 比如我根据用户的id查找用户    调用SqlSession调用增删改查的方法时, 由于该方法的变量类型使用的泛型 ,这就在编译阶段,即使类似错误,也不会报错,不利于开发维护 二:mapper代理开发---->相当于一个dao接口(其实本来就是一个接口。。。)      2.1.1 思路(要遵循mapper代理开发规范) 程序员还需要编写 mapper.xml 映射文件 程序员编写 mapper 接口需要遵循一些开发规范, mybatis 可以自动生成 mapper 接口实现类代理对象。 开发规范:      2.1.1.1 映射文件 namespace --->对应UserMapper的接口地址   2.1.1.2 UserMapper的接口   2.1.1.2 测试类    来源: https://www.cnblogs.com/Yzengxin/p/11403283.html

Is there a way to change the JPA fetch type on a method?

余生长醉 提交于 2019-11-28 08:19:22
Is there a way to change the JPA fetch type on a single method without editing the entity object? I have a shared ORM layer consisting of JPA entity classes. This ORM layer is accessed by two DAO layers. One DAO needs lazy fetching, as it is for my web application, the other needs eager fetching, as I need it to be threadsafe. Here is an example method from my threadsafe DAO, @PersistenceContext(unitName = "PersistenceUnit", type = PersistenceContextType.TRANSACTION) private EntityManager em; public ErrorCode findErrorCodeById(short id) { return (ErrorCode) em.createNamedQuery("ErrorCode

How to create database from Assets in Android using GreenDao ORM library

早过忘川 提交于 2019-11-28 07:05:30
I am using GreenDao orm library for creating database, it works good with the First sample for Creating Database, from within the code. Now the problem is I can't find any documentation or anything related to the Database copying from Assets Folder to the databases inside memory. The accepted answer provides a solution but it's only partially correct. Check out the class I posted below. This is an extension of greenDao's OpenHelper class. Below that is an example of it's use and how you can use it instead of OpenHelper or DevOpenHelper to manage the copy of your sqlite database. package com

Multiple Entity Manager issue in Spring when using more than one datasource

故事扮演 提交于 2019-11-28 07:05:15
I have two entity managers in my applicationContext.xml which corresponds to two different databases. I can easily query database1 with entityManager1 , but when I try to access database2 with entityManager2 , I am not getting any results. I am using Spring+Hibernate+JPA. Here is my ApplicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema

springData表关系:一对多

社会主义新天地 提交于 2019-11-28 05:45:53
一、编写实体类进行表关联 1、在一张表的关联属性上添加@OneToMany注解(关联属性用来记录多的一方的信息,是个集合,一般用set) 2、在另一个实体类的关联属性上添加@ManyToOne注解和 @JoinColumn(name="custid",referencedColumnName = "cust_id")注解 二、编写dao:两个dao都继承JpaRepository<实体类名,主键属性> 三、编写测试类(用法) package cn.lijun.jpa; import cn.lijun.jpa.dao.CustomerDao2; import cn.lijun.jpa.dao.LinkManDao; import cn.lijun.jpa.entity.Customer2; import cn.lijun.jpa.entity.LinkMan; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Commit; import org.springframework.test.context