练习练习
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;
}
}
<?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;
}
}
相关文章推荐
- 网管的要求!从现在起一步一步做!
- bootstrap学习笔记(学习中。。。)
- android 面试小知识
- T-SQL 查询、修改数据表
- IdHttpServer实现webservice(130篇DataSnap文章)
- Directx11学习笔记【五】 基本的数学知识----向量篇
- 蓝牙利用协议栈API读取设备MAC地址
- 监控利器---Zabbix(一)
- 浅谈上下文(context)
- WebApi
- 联调扯皮的分歧解决办法------抓包(wireshark, tcpdump, fiddler)
- KMP POJ 1961 Period 解题报告
- 1087. All Roads Lead to Rome (30)
- centOs安装搭建php环境
- 第三百三十五天 how can I 坚持
- LCS(Longest Common Subsequence)算法
- getActionBar为null的解决办法
- 数据类型---原始数据类型
- eclipse中安装genymotion教程
- JS-表单提交检查表单字数方法