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. “全栈2019”Java异常第七章:try-catch-finally组合方式

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  2. Windows Python Extension Packages

    备注: 1.先要安装wheel库:pip install wheel 2.下载wheel,切换至下载路径,然后安装:pip install wheel库名.whl Windows Python Ext ...

  3. Metasploit渗透某高校域服务器

    本文作者:i 春秋签约作家——shuteer 前期准备:1. 使用Veil生成免杀PAYLOAD: 2. 有一个外网IP的服务器,安装好metasploit,方便操作. 一.SHELL反弹meterp ...

  4. DBHelper--Java JDBC SSH 连接数据库工具类

    概述 JDBC 指 Java 数据库连接,是一种标准Java应用编程接口( JAVA API),用来连接 Java 编程语言和广泛的数据库. ----------------------------- ...

  5. java使用Redis8--3.0集群

    Redis集群至少需要3个主节点 # cd /usr/redis 创建一个目录 # mkdir cluster # cd cluster 1.复制一个配置文件 # cp ../redis.conf 9 ...

  6. jdk命令行工具(一)

    1.概述 熟悉java开发的人应该都知道在jdk的bin目录下有许多的工具,这些工具主要用于监视虚拟机和故障处理.这些故障处理工具被Sun公司称作为“礼物”附赠给JDK的使用者,并在软件的使用说明中把 ...

  7. extjs Tree中避免连续单击会连续请求服务器

    应用场景:在项目中我要做一个左边是tree,右边是panel的界面.当我单击tree中的一条记录时,发送请求,并将结果显示在右边的panel中.做完之后发现,如果连续单击就会连续请求两次服务器,毕竟用 ...

  8. ES6之新增let命令使用方法

    let命令的用法 let是es6中的声明一个变量的命令,只在它声明的代码块中有效,出了这个代码块就会报错.也非常适合for循环,在循环中i的值只在循环语句中生效,在外边取不到的. var命令声明的是一 ...

  9. windows 下加载执行hta文件的方法

    首先编写这么一个hta的文件: <html> <head> <script> s = new ActiveXObject("WScript.Shell&q ...

  10. jquery中ajax使用error调试错误的方法,实例分析了Ajax的使用方法与error函数调试错误的技巧

    代码:$(document).ready(function() { jQuery("#clearCac").click(function() { jQuery.ajax({ url ...