06 DAO層と増加問い合わせページネーション

前の記事では、我々はウェブのデカップリング層とサービス層、2つのだけの層における我々の注意の焦点を記述しますが、実際のプロジェクトでは、確かにデータベースへのアクセスが必要になります。そこで、この記事では、データのクエリロジックが土台の前に追加される方法を説明します。

1、環境制約

  • win10 64オペレーティングシステム
  • idea2018.1.5
  • maven-3.0.5
  • JDK-8u162-Windowsのx64の

2、ソフトウェアのダウンロード

ネットワークディスクBaiduの:
リンク:https://pan.baidu.com/s/1gfnI8NqUYgYK1g0ULGIV2w
抽出コード:q9pl

3、拘束の前提

  • 飼育係とダボがインストールされ、開始されました
  • このアルバムは完全にやって、「Webサービス層[消費]を作成して起動する05」になります。
  • MySQLはインストールして、次のSQL文を実行されています:
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、ステップ:

4.1最初のステップ:ことを確認してください関連のjarパッケージが依存して参加していMyBatisの[注:最も外側の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ステップ2:新しいFBS-DAOを作成します。

  • 次の依存関係は、FBSのpom.xml-DAOモジュールに追加されます。
        <dependency>
            <groupId>net.wanho.fenbushi</groupId>
            <artifactId>fbs-pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
  • SRC /メイン/ジャワにおけるFBS-DAOとnet.wanho.fenbushi.mapperパッケージを作成し、パッケージには、間UserMapper.javaカテゴリを追加しました
package net.wanho.fenbushi.mapper;

import net.wanho.fenbushi.pojo.User;

import java.util.List;

public interface UserMapper {
    List<User> query();
}
  • FBS-DAOはパケットnet.wanho.fenbushi.mapperするUserMapper.xmlを追加しました
<?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>
  • 修正FBS-のDAOのpom.xmlがされ
    、ビルドラベルを追加しました:
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

4.3第三ステップ:修正FBSサービス

  • FBS-サービスのpom.xmlを修正し、依存の追加DAO層
        <dependency>
            <groupId>net.wanho.fenbushi</groupId>
            <artifactId>fbs-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  • net.wanho.fenbushi.service.impl.UserServiceImpl.javaの修正FBSサービス
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;
    }
}
  • データベースを増やし、およびコンフィギュレーションファイル内のsrc /メイン/リソースのFBS-サービスにおけるトランザクションページ
    のApplicationContext-dao.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"
       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>
  • 確認FBS-サービス層は、親コンテナのxmlファイルのすべてを読み込むことができます
<!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修正FBS-ウェブ

net.wanho.fenbushi.controller.UserController.javaの修正FBS-ウェブ

5、テストを開始

飼育係の開始を確認し、ダボスタート、FBS-サービスを開始し、その後、FBS-ウェブを開始し、ブラウザでのポイント:HTTP:// localhostの:? 8082 /ユーザー/クエリPAGENUM = 1&pageSizeを= 2
この時点で、我々は分散型のプロジェクトで完了しましたデータ照会モジュールを増加させました。

おすすめ

転載: www.cnblogs.com/alichengxuyuan/p/12581422.html