MiniDao (JAVA lightweight persistence layer, Hibernate perfect assistant)

MiniDao (JAVA lightweight persistence layer, Hibernate perfect assistant)

Current Latest Version: 1.6.2 (Release date: 20180309)

MiniDao produce original intention?

Using Hibernate J2EE project has a painful disease for complex business SQL, lack of ability to hibernate, SQL optimization can not be separated from bad. This time I thought integrated mybatis, but a project with both hibernate and with mybatis, seem very heavy transaction is not well controlled. It springjdbc conventional practice is to use native SQL written to achieve, but there are also a problem, SQL can not be separated nor logical label capability. So in order to solve this painful disease, Jeecg for springjdbc + freemarker made a package out of such a lightweight persistence layer, allowing Hiberate have mybatis as flexible SQL capability, while supporting the transaction unified, SQL label capability.

MiniDao Introduction and features

JAVA is a lightweight MiniDao persistence framework, SpringJdbc + freemarker Based includes separating same Mybatis SQL labels and logic capabilities. Minidao produce original intention was to solve the Hibernate project, a complex SQL with the same Mybatis flexible capability, while supporting transaction synchronization.

It has the following characteristics:

  • O / R mapping not set xml, easy to maintain zero configuration
  • Knowledge without knowing JDBC
  • Separate SQL statements and java code
  • Just interface definition without interface
  • SQL supports scripting languages ​​(powerful scripting language, freemarker grammar)
  • Lightweight support and seamless integration hibernate
  • It supports automatic and manual transaction processing transaction processing
  • Performance is better than Mybatis
  • SQL tags using the basic syntax of Freemarker

Interface Definition [EmployeeDao.java]

@MiniDao
public interface EmployeeDao {

 @Arguments({ "employee"})
 @Sql("select * from employee")
 List<Map<String,Object>> getAll(Employee employee);

 @Sql("select * from employee where id = :id")
 Employee get(@Param("id") String id);

 @Sql("select * from employee where empno = :empno and  name = :name")
 Map getMap(@Param("empno")String empno,@Param("name")String name);

 @Sql("SELECT count(*) FROM employee")
 Integer getCount();

 int update(@Param("employee") Employee employee);

 void insert(@Param("employee") Employee employee);
 
 @ResultType(Employee.class)
 public MiniDaoPage<Employee> getAll(@Param("employee") Employee employee,@Param("page")  int page,@Param("rows") int rows);    }

SQL File [EmployeeDao_getAllEmployees.sql]

SELECT * FROM employee where 1=1 
<#if employee.age ?exists>
and age = :employee.age
</#if>
<#if employee.name ?exists>
and name = :employee.name
</#if>
<#if employee.empno ?exists>
and empno = :employee.empno
</#if>

### corresponding to the file directory and SQL interfaces

github

MiniDao configuration in the spring

<!-- MiniDao动态代理类 -->
<bean id="miniDaoHandler" class="org.jeecgframework.minidao.factory.MiniDaoBeanScannerConfigurer">
	<!-- 是使用什么字母做关键字Map的关键字 默认值origin 即和sql保持一致,lower小写(推荐),upper 大写 -->
	<property name="keyType" value="lower"></property>
	<!-- 格式化sql -->
	<property name="formatSql" value="false"></property>
	<!-- 输出sql -->
	<property name="showSql" value="false"></property>
	<!-- 数据库类型 -->
	<property name="dbType" value="mysql"></property>
	<!-- dao地址,配置符合spring方式 -->
	<property name="basePackage" value="examples.dao.*"></property>
	<!-- 使用的注解,默认是Minidao,推荐 Repository-->
	<property name="annotation" value="org.springframework.stereotype.Repository"></property>
	<!-- Minidao拦截器配置 	-->
	<property name="emptyInterceptor" ref="minidaoInterceptor"></property>
</bean>

Test code

public class Client {
public static void main(String args[]) {
	BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
 		
	EmployeeDao employeeDao = (EmployeeDao) factory.getBean("employeeDao");
	Employee employee = new Employee();
	String id = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
	employee.setId(id);
	employee.setEmpno("A001");
	employee.setSalary(new BigDecimal(5000));
	employee.setBirthday(new Date());
	employee.setName("scott");
	employee.setAge(25);
	//调用minidao方法插入
	employeeDao.insert(employee);
}
}

Technology Exchange

Guess you like

Origin www.cnblogs.com/gaobing1252/p/11110593.html
Recommended