您的位置:首页 > 其它

练习练习

2016-03-02 23:13 316 查看

<?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">
 

 

 <!-- 开启IOC注解扫描 -->

 <context:component-scan base-package="org.tarena.note"/>

 

 

 <!-- 开启SpringMVC注解扫描 -->

 <mvc:annotation-driven/>

 

 <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">

  <property name="username" value="root"/>

  <property name="password" value="123456"/>

  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

  <property name="url" value="jdbc:mysql:///cloud_note?useUnicode=true&characterEncoding=utf8"/>

 </bean>

 

 <bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">

  <property name="dataSource" ref="dbcp"/>

  <property name="mapperLocations" value="classpath:org/tarena/note/sql/*.xml"/>

 </bean>

 

 

 <!-- 根据Dao接口生成接口对象 -->

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

  <property name="basePackage" value="org.tarena.note.dao"/>

 </bean>

</beans><?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">

<!-- 开启IOC注解扫描 -->

<context:component-scan base-package="org.tarena.note"/>

<!-- 开启SpringMVC注解扫描 -->

<mvc:annotation-driven/>

<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">

<property name="username" value="root"/>

<property name="password" value="123456"/>

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql:///cloud_note?useUnicode=true&characterEncoding=utf8"/>

</bean>

<bean id="sqlsessionfactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dbcp"/>

<property name="mapperLocations" value="classpath:org/tarena/note/sql/*.xml"/>

</bean>

<!-- 根据Dao接口生成接口对象 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="org.tarena.note.dao"/>

</bean>

</beans>

web.xml内容

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

 xmlns="http://java.sun.com/xml/ns/javaee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

 <servlet>

  <servlet-name>springmvc</servlet-name>

  <
4000
servlet-class>

   org.springframework.web.servlet.DispatcherServlet

  </servlet-class>

  <init-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:applicationContext.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>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

dao文件

package org.tarena.note.dao;

import java.util.List;

import org.tarena.note.entity.NoteBook;

public interface NoteBookDao {

 /**

  * 根据用户Id查询笔记本信息

  * @param userId 用户ID

  * @return 笔记本集合

  */

 public List<NoteBook> findByUserId(String userId);

 

 public int save(NoteBook noteBook);

 

 public NoteBook findByNameAndUserId(NoteBook book);

}

service文件

package org.tarena.note.service;

import org.tarena.note.entity.NoteResult;

public interface BookService {

 public NoteResult loadBooksByUserId(String userId);

 

 public NoteResult create(String userId,String bookName);

 

}

impl文件

package org.tarena.note.service.impl;

import java.sql.Timestamp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.tarena.note.dao.NoteBookDao;

import org.tarena.note.entity.NoteBook;

import org.tarena.note.entity.NoteResult;

import org.tarena.note.service.BookService;

import org.tarena.note.util.NoteUtil;

@Service

public class BookServiceImpl implements BookService {

 @Autowired

 private NoteBookDao bookDao;

 

 public NoteResult loadBooksByUserId(String userId) {

  NoteResult result = new NoteResult();

  if(userId != null && !"".equals(userId)){

   List<NoteBook> list = bookDao.findByUserId(userId);

   result.setData(list);

  }

  

  result.setStatus(0);

  result.setMsg("加载笔记本成功");

  

  return result;

 }

 public NoteResult create(String userId, String bookName) {

  //检查参数格式

  NoteResult result = new NoteResult();

  if(bookName==null || "".equals(bookName)){

   result.setStatus(1);

   result.setMsg("笔记本名称为空");

   return result;

  }

  System.out.println(bookName+" "+userId);

  //添加操作

  NoteBook book = new NoteBook();

  book.setCn_user_id(userId);

  book.setCn_notebook_name(bookName);

  System.out.println(book);

  NoteBook nbook = bookDao.findByNameAndUserId(book);

  System.out.println(nbook);

  if(nbook != null){

   result.setStatus(1);

   result.setMsg("笔记本名被占用");

   

   return result;

  }

  String bookId = NoteUtil.createId();

  book.setCn_notebook_id(bookId);

  book.setCn_notebook_type_id("5");

  book.setCn_notebook_desc("");

  long l = System.currentTimeMillis();

  Timestamp time = new Timestamp(l);

  book.setCn_notebook_createtime(time);

  bookDao.save(book);

  result.setStatus(0);

  result.setMsg("添加成功");

  result.setData(bookId);

  return result;

 }

}

controller文件

package org.tarena.note.web.controller.book;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.tarena.note.entity.NoteResult;

import org.tarena.note.service.BookService;

@Controller

@RequestMapping("/notebook")

public class LoadBooksController {

 @Autowired

 private BookService bookService;

 

 @RequestMapping("/loadbooks")

 @ResponseBody

 public NoteResult execute(String userId){

  NoteResult result = bookService.loadBooksByUserId(userId);

  return result;

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息