MyBatisjava版云笔记(一)

开口笔记型
夫类型之sql文件,需求文档,需要的html文件,jar包都得以错过下载,下载地址为:http://download.csdn.net/download/liveor_die/9985846

类简介

记管理网,用户可以管理笔记信息,可以翻
其他用户分享的笔记.

重在功效如下

  • 用户模块:登录、注册、修改密码、退出
  • 笔记本模块:创建、删除、更新、查看
  • 记模块:创建、删除、更新、查看、转移
  • 享受和储藏模块:分享、收藏、查看、搜索分享
  • 回收站模块:查看、彻底剔除、恢复
  • 活动模块:查看活动、参加活动等

型总体计划

使用的机要技术

jQuery、Ajax、SpringMVC、IOC、AOP、MyBatis

  • jQuery:简化前端javascript和ajax编程
  • Ajax:页面局部处理;提升用户体验和性
  • SpringMVC:负责接请求,调用业务组件处理,
    拿结果生成JSON响应输出
  • SpringIOC:负责管理Controller,Service,Dao;维护这些零件对象期间的涉
  • MyBatis:负责贯彻数据库操作,实现Dao
  • SpringAOP:负责作业和生日志功能切入。
    (不用修改原有组件代码,就好加功能)

项目完全标准

-所有请求采用Ajax方式相互
(使用$.ajax()函数)
-系统页面全部动HTML
(替代JSP+JSTL+EL)
-所有请求服务器处理了回到的JSON结果格式
如下

{“status”:xx,”msg”:xxx,”data”:xxx}

  表现层–》控制层–》业务层–》持久层/数据访问层
  HTML(ajax)–>Controller–>Service–>Dao

应流程

Ajax+SpringMVC+Spring(IOC/AOP)+MyBatis
Ajax–>SpringMVC–>返回JSON结果

JSP响应流程

请求–>DispatcherServlet–> HandlerMapping–> Controller–
返回ModelAndView/String–> ViewResolver–> JSP –>响应

JSON响应流程

请–>DispatcherServlet–> HandlerMapping–>
Controller–> 返回数据对象(int,User,List,Map)–>
引入jackson包,于Controller方法前加加@ResponseBody标记–>
JSON响应

搭建环境

创建mysql数据库,名字为:cloud_note,编码格式为utf-8

数据库文件下充斥地址:

MyBatis 1
mysql常因此数据库语句

  • show databases;//查看有安库
  • use 库名;//使用某库
  • show tables;//查看库里发出怎样表
  • desc 表名;//查看表结构
  • source sql文件;//导入sql文件
  • 导入项目数据库步骤

-set names utf8;

-source D:\cloud_note.sql;

详尽的有关mysql的sql可以关押我的关于mysql的博客。

创建maven web项目

项目名字也:cloud_note

欲引入的保险

  • spring-webmvc包
  • mybatis包
  • dbcp包+MySQL驱动包
  • jackson包
  • mybatis-spring.jar
  • junit包

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</version>
    </dependency>
  </dependencies>

配置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">
  <display-name>cloud_note</display-name>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <!-- dispatcherServlet的初始化方法会启动
        spring容器,所以需要告诉它spring配置文件的位置

        注意:默认情况下,如果没有配置contextconfiglocation(既没有指定spring指定配置
        文件的位置则会查找web-inf/servletname-servlet.xml文件
     -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/spring-*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

安排spring配置文件

spring-mvc.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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 配置handlermapping -->
    <mvc:annotation-driven/>
    <!-- 启动扫描controller -->
    <context:component-scan base-package="com.tedu.cloudnote"/>
</beans>

spring-mybatis.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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- 配置dbcp组件DataSource -->

    <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/cloud_note">
        </property>
    </bean>
    <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dbcp">

        </property>
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    <!-- 定义MapperScannerConfigurer -->
    <bean id="mapperScanner"
        class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.tedu.cloudnote.dao"></property>

        <!-- 可以省略不写 -->
        <property name="sqlSessionFactory" ref="ssf"></property>
    </bean>
</beans>

划分包结构

  • com.tedu.cloudnote.controller
  • com.tedu.cloudnote.controller.user
  • com.tedu.cloudnote.controller.book
  • com.tedu.cloudnote.controller.note
  • com.tedu.cloudnote.service
  • com.tedu.cloudnote.dao
  • com.tedu.cloudnote.entity
  • com.tedu.cloudnote.util

MyBatis 2

至此地路的环境就是搭建好了。下同样节约开始写项目。

网站地图xml地图