06 dao layer and increase query pagination

Previous articles, we describe a web decoupling layer and the service layer, the focus of our attention only in two layers, but in a real project which will certainly require database access. Therefore, this article describes how the data query logic is added in front of the foundation.

1, environmental constraints

  • win10 64 operating system
  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64

2, software download

Baidu network disk:
Link: https://pan.baidu.com/s/1gfnI8NqUYgYK1g0ULGIV2w
extraction code: q9pl

3, the premise of restraint

  • zookeeper and dubbo has been installed and started
  • This album will be "05 to create a web service layer [consumer] and start" doing complete
  • mysql has been installed, and execute the following sql statement:
use test;
create table t_user(id int ,name varchar(20));
insert into t_user(id,name) values(1,'zhangsan');
insert into t_user(id,name) values(2,'lisi');
insert into t_user(id,name) values(3,'wangwu');
insert into t_user(id,name) values(4,'ali');
insert into t_user(id,name) values(5,'xiaoili');
insert into t_user(id,name) values(6,'zhangli');

4, the steps:

4.1 The first step: Make sure mybatis related jar package has joined rely [Note: the outermost pom.xml]

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>${mybatis.paginator.version}</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

4.2 Step Two: Create a new fbs-dao

  • The following dependencies are added in pom.xml fbs-dao module:
        <dependency>
            <groupId>net.wanho.fenbushi</groupId>
            <artifactId>fbs-pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
  • Fbs-dao in the src / main / java and create a net.wanho.fenbushi.mapper package, the package added UserMapper.java category among
package net.wanho.fenbushi.mapper;

import net.wanho.fenbushi.pojo.User;

import java.util.List;

public interface UserMapper {
    List<User> query();
}
  • Fbs-dao added UserMapper.xml in which the packet net.wanho.fenbushi.mapper
<?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="net.wanho.fenbushi.mapper.UserMapper">
    <select id="query" resultType="net.wanho.fenbushi.pojo.User">
        select * from t_user
    </select>
</mapper>
  • Modify fbs-dao pom.xml is
    added build labels:
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

4.3 The third step: Modify fbs-service

  • Fbs-service modify the pom.xml, dependent added dao layer
        <dependency>
            <groupId>net.wanho.fenbushi</groupId>
            <artifactId>fbs-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  • Modify fbs-service of net.wanho.fenbushi.service.impl.UserServiceImpl.java
package net.wanho.fenbushi.service.impl;

import net.wanho.fenbushi.mapper.UserMapper;
import net.wanho.fenbushi.pojo.User;
import net.wanho.fenbushi.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> queryUsers() {
        List<User> list = userMapper.query();
        return list;
    }
}
  • Increase the database, and transaction pages in fbs-service of src / main / resources in the configuration file
    applicationContext-dao.xml [Note: The database account password, please change the actual situation]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置数据库连接池,使用的是druid,数据库的实例、账号、密码,请根据实际情况修改-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="zhangli"></property>
    </bean>

    <!--配置SqlSession的工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation"  value="classpath:Mybatis-Config.xml"/>
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置数据库操作的接口以及xml所在package-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="net.wanho.fenbushi.mapper"></property>
    </bean>
</beans>

applicationContext-trans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--设置事务管理器,引用的是applicationContext-dao.xml中配置的datasource-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置事务通知,引用上面设置的事务管理器
        add开头的方法需要事务
        其他方法都是只读的
    -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!--定义连接点,advisor定义了通知在连接点上执行即织入-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* net.wanho.fenbushi.service.*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

Mybatis-Config.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>
<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    </plugin>
</plugins>
</configuration>
  • Confirm fbs-service layer can load all of the parent container xml file
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-*.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

4.4 modify fbs-web

Modify fbs-web of net.wanho.fenbushi.controller.UserController.java

5, start the test

Ensure zookeeper start, dubbo start, start fbs-service, and then start fbs-web, point your browser to: HTTP: // localhost:? 8082 / the User / Query pageNum = 1 & pageSize = 2
At this point, we have completed in distributed projects increased data query module.

Guess you like

Origin www.cnblogs.com/alichengxuyuan/p/12581422.html