dao层配置

dao层配置注意事项:

  1、Mapper.xml 文件中的 namespace 与 mapper 接口的类路径相同

  2、Mapper.xml 接口方法名和 Mapper.xml 中定义的每个 statement 的id相同

  3、Mapper 接口方法的输入参数类型和 mapper.xml 中定义的每个sql的 paramenterType的类型相同

  4、Mapper 接口方法的输出参数类型和 mapper.xml 中顶一个的每个sql的 resultType 的类型相同

1. SqlMapConfig.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>
<!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
2. db.properties
  连接mysql数据库所需要的信息
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/pinyougoudb?characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=pig
3. applicationContext-dao.xml
  <!-- 数据库连接池 -->
   <!-- 加载配置文件 -->
   <context:property-placeholder location="classpath:properties/db.properties"/>
   <!-- 数据库连接池 -->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
         destroy-method="close">
       <property name="url" value="jdbc:mysql://localhost:3306/taobao?characterEncoding=utf-8"/>
       <property name="username" value="root"/>
       <property name="password" value="pig"/>
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="maxActive" value="10"/>
       <property name="minIdle" value="5"/>
   </bean>

   <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 数据库连接池 -->
       <property name="dataSource" ref="dataSource"/>
       <!-- 加载mybatis的全局配置文件 -->
       <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
   </bean>
   <!--配置mapper映射路径-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.taobao.mapper"/>
   </bean>

service层配置

注意事项: 1、使用注解配置bean注入到ioc容器。一定要记得添加组件扫描,否则会报出无法创建service bean的错误 2、事务控制注解使用@Transactional注解配置,可以作用在类和方法上


  <?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:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <!--组件扫描-->
   <context:component-scan base-package="com.taobao.service"/>
   <!-- 事务管理器 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource" />
   </bean>

   <!-- 开启事务控制的注解支持 -->
   <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

表现层配置

注意事项: 1、web.xml中需要配置监听器加载spring配置文件,配置解决post乱码,配置springmvc前端控制器DispartcherServlet 2、springmvc配置文件中记得添加组建扫描,否则会报出无法创建bean错误

    applicationContext.xml中导入dao和service的配置
   <!--加载service中spring配置文件-->
   <import resource="classpath*:spring/applicationContext-dao.xml"/>


   <!--加载service中spring配置文件-->
   <import resource="classpath*:spring/applicationContext-tx.xml"/>

springmvc.xml中配置下面一句就行。
1. controller使用@ResponseBody声明在方法上,或者直接在类上声明@RestController(相当于@Controller和@ResponseBody的结合)
2. 前端给后台的数据是springmvc自动封装到参数中的。如果需要json数据,可以声明@RequestBody。后台给前端的数据是JSON格式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      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
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">

   <!--组件扫描-->
   <context:component-scan base-package="com.taobao.controller"/>

   <!--配置视图解析器-->
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/pages/"/>
       <property name="suffix" value=".html"/>
   </bean>


   <!--释放静态资源-->
   <mvc:default-servlet-handler/>

   <!--处理器映射器、处理器适配器-->
   <!---配置JSON解析器-->
   <mvc:annotation-driven>
 <mvc:message-converters register-defaults="true">
   <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
     <property name="supportedMediaTypes" value="application/json"/>
     <property name="features">
       <array>
         <value>WriteMapNullValue</value>
         <value>WriteDateUseDateFormat</value>
       </array>
     </property>
   </bean>
 </mvc:message-converters>
</mvc:annotation-driven>

</beans>
web.xml配置


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

   <!--spring security过滤器-->
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring/spring-security.xml</param-value>
   </context-param>
   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <filter>
       <filter-name>springSecurityFilterChain</filter-name>
       <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>springSecurityFilterChain</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>


     <!-- 加载spring容器 -->
   <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:spring/applicationContext*.xml</param-value>
   </context-param>
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <!-- 解决post乱码 -->
   <filter>
       <filter-name>CharacterEncodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>utf-8</param-value>
       </init-param>
       <init-param>
           <param-name>forceEncoding</param-name>
           <param-value>true</param-value>
       </init-param>
   </filter>
   <filter-mapping>
       <filter-name>CharacterEncodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

 <!---配置前端控制器加载springMVC核心配置文件-->
   <servlet>
       <servlet-name>springmvc</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:spring/springmvc.xml</param-value>
       </init-param>
   </servlet>

   <servlet-mapping>
       <servlet-name>springmvc</servlet-name>
       <url-pattern>*.do</url-pattern>
   </servlet-mapping>

</web-app>
注解说明:

@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法。通俗来说,被Controller标记的类就是一个控制器,这个类中的方法,就是相应的动作。
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。比如图一中,跳转到登录页面的路径就是localhost:8080/xxx-war/user/toLogin
service采用@service注解


例:@Service("userService")注解是告诉Spring,当Spring要创建UserServiceImpl的的实例时,bean的名字必须叫做"userService",这样当Action需要使用UserServiceImpl的的实例时,就可以由Spring创建好的"userService",然后注入给Action。
dao层使用@repository注解

@Controller
@ResponseBody 添加此注解表示返回类型是JSON格式
@RestController 相当于@Controller和@ResponseBody的结合
1、@RequestBody 作用于方法的参数上,传入的参数类型必须是String。因为传入的数据类型是JSON字符差串数据

2、如果作用了@ResponseBody注解或者直接在类上使用了@RestController注解则返回类型是JSON格式。(不走视图解析器)
如果没有注明返回的是JSON数据,则返回数据会走视图解析器


java中的注解大全@controller、@service、@repository等
引用前辈的连接(https://www.cnblogs.com/CrisZjie180228/p/8745603.html)

ssm项目快速搭建(注解)的更多相关文章

  1. ssm项目快速搭建(注解)-依赖

    父层jar包版本控制,管理配置 <!-- 集中定义依赖版本号 -->    <properties>        <junit.version>4.12</ ...

  2. ssm项目快速搭建(配置)

    核心jar包 <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncod ...

  3. SSM 项目从搭建爬坑到 CentOS 服务器部署 - 速查手册

    SSM 项目从搭建爬坑到 CentOS 服务器部署 - 速查手册 提示: (1)CSDN 博客左边有操作工具条上有文章目录 (2)SSM 指 Spring,Spring MVC,MyBatis Mav ...

  4. ssm项目框架搭建(增删改查案例实现)——(SpringMVC+Spring+mybatis项目整合)

    Spring 常用注解 内容 一.基本概念 1. Spring 2. SpringMVC 3. MyBatis 二.开发环境搭建 1. 创建 maven 项目 2. SSM整合 2.1 项目结构图 2 ...

  5. 新巴巴运动网上商城 项目 快速搭建 教程 The new babar sports online mall project quickly builds a tutorial

    新巴巴运动网上商城 项目 快速搭建 教程 The new babar sports online mall project quickly builds a tutorial 作者:韩梦飞沙 Auth ...

  6. springboot项目快速搭建

    1. 问题描述 springboot的面世,成为Java开发者的一大福音,大大提升了开发的效率,其实springboot只是在maven的基础上,对已有的maven gav进行了封装而已,今天用最简单 ...

  7. maven项目快速搭建SSM框架(一)创建maven项目,SSM框架整合,Spring+Springmvc+Mybatis

    首先了解服务器开发的三层架构,分配相应的任务,这样就能明确目标,根据相应的需求去编写相应的操作. 服务器开发,大致分为三层,分别是: 表现层 业务层 持久层 我们用到的框架分别是Spring+Spri ...

  8. SSM项目的搭建

    本文示例在如下环境下搭建一个Maven+Druid+SSM+Shiro+Mysql+PageHelper以及Mybatis Generator反向生成代码的项目 附上GitHub地址:https:// ...

  9. SSM框架快速搭建

    1.   新建Maven项目 ssm 2.    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...

随机推荐

  1. 洛谷P5280 [ZJOI2019]线段树(线段树)

    题面 传送门 题解 考场上就这么一道会做的其它连暴力都没打--活该爆炸-- 首先我们得看出问题的本质:有\(m\)个操作,总共\(2^m\)种情况分别对应每个操作是否执行,求这\(2^m\)棵线段树上 ...

  2. Dubbo 自定义异常,你是怎么处理的?

    前言 记录Dubbo对于自定义异常的处理方式. 实现目标 服务层异常,直接向上层抛出,web层统一捕获处理 如果是系统自定义异常,则返回{"code":xxx,"msg& ...

  3. dbporxy-mysql 协议流转图

    dbproxy 支持 in 查询, 当in 中的字段 属于不同的分表时, QPS约为 5000左右, 如果为 等值查询,  qps的30000左右 主要原因是 对于in操作,会产生多个不同分表的sql ...

  4. FlowPortal-BPM——管理员、功能的权限设置

    一.管理员设置 管理工具→安全组→安全组名称→管理授权→[添加管理人员]→[设置管理人员权限] 二.访问功能权限设置 (1)模块访问权限 (2)访问控制→[在需要的文件夹下]新建子资源→[资源名称]. ...

  5. 【性能调优】:记录一次数据库sql语句性能调优过程

    一,依旧很简单的一个接口,查询列表接口,发现10并发单交易场景下,数据库表4w铺底数据,每次查询2000条数据进行orderby显示,平均响应时间2秒以上,数据库的cpu使用率高达95%: 二,抓到这 ...

  6. [转] flume使用(六):后台启动及日志查看

    [From] https://blog.csdn.net/maoyuanming0806/article/details/80807087 处理的问题flume 普通方式启动会有自己自动停掉的问题,这 ...

  7. 用python开发了一个简单apache web服务端范例,在win10 + apache2.4.9 + python3.5 测试成功

    #!D:\Programs\Python\Python35-32\python.exe import cgi def htmlTop():     print("Content-type: ...

  8. 安装TD出现Unknown user name or bad password问题

    在Server 2003 sp2上安装TD8.0 出现Unknown user name or bad password,是因为2003启用了DEP保护.  关闭系统的DEP保护就可以了.  方法如下 ...

  9. 高性能的数据压缩库libzling-20160105

    libzling(https://github.com/richox/libzling,求观看[watch],求星[star],求叉[fork])是一款高性能的数据压缩库,参见原贴:http://ww ...

  10. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...