您的位置:首页 > 编程语言 > Java开发

SpringMVC+mybatis+Spring框架整合+简单实现的demo

2014-11-19 19:25 573 查看
这次可能不会把整个项目的代码都贴出来!!

1.代码结构:



2.亮一下部分代码:

applicationContext-mapper.xml:
<bean id="UserMapper" class="org.mybatis.spring.MapperFactoryBean">
<property name="mapperInterface" value="com.goodtaste.dao.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

applicationContext-service.xml:
<bean id="UserService" class="com.goodtaste.service.impl.UserService">
<property name="userMapper" ref="UserMapper"></property>
</bean>

applicationContext-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 注解扫描包 -->
<context:component-scan base-package="com.goodtaste.*"></context:component-scan>

<!-- 开启注解 -->
<mvc:annotation-driven />
<!-- 静态资源访问 -->

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
lazy-init="false" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- <bean name="/testSSH" class="com.tdd.ssh.controller.UserController"
/> -->
<!-- 支持文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<!--     告诉视图解析器,返回的类型为json格式 -->
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!--  ModelAndView里的数据变成JSON -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
</beans>

applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> <context:component-scan base-package="com.*" />
<context:annotation-config/>

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="50"/>
<property name="maxIdleTime" value="1800"/>
<property name="acquireIncrement" value="2"/>
<property name="maxStatements" value="0"/>
<property name="initialPoolSize" value="10"/>
<property name="idleConnectionTestPeriod" value="30"/>
<property name="acquireRetryAttempts" value="30"/>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:configuration.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>

<import resource="applicationContext-*.xml"/>
<import resource="configuration.xml"/>
</beans>

configuration.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>
<typeAliases>
<!--给实体类起一个别名 user -->
<typeAlias type="com.goodtaste.model.User" alias="User" />
<typeAlias type="com.goodtaste.model.Group" alias="Group" />
<typeAlias type="com.goodtaste.model.Dishes" alias="Dishes" />
<typeAlias type="com.goodtaste.model.SystemLog" alias="SystemLog"/>
<typeAlias type="com.goodtaste.model.Order" alias="Order"/>
<typeAlias type="com.goodtaste.model.MenuCsp" alias="MenuCsp"/>

</typeAliases>
<mappers>
<!--userMapper.xml装载进来 同等于把“dao”的实现装载进来 -->
<mapper resource="com/goodtaste/dao/impl/UserMapper.xml" />
<mapper resource="com/goodtaste/dao/impl/GroupMapper.xml" />
<mapper resource="com/goodtaste/dao/impl/DishesMapper.xml" />
<mapper resource="com/goodtaste/dao/impl/SystemLogMapper.xml" />
<mapper resource="com/goodtaste/dao/impl/OrderMapper.xml" />
<mapper resource="com/goodtaste/dao/impl/MenuCspMapper.xml" />
</mappers>
</configuration>

jdbc.properties:
#MYSQL
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.jdbcUrl=jdbc\:mysql\://localhost\:3306/goodtaste
jdbc.username=root
jdbc.password=111111

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/register.js"></script>
</head>
<body>
</html>
register.js:
$(function(){
$.getJSON(
"/goodtaste_system/RegisterUser.do",
{
userName:"luoxue",
passWord:"123qwe",
groupId:1
},
function(data){
if(data.success){
alert("注册!");
//     alert(data.userId);
}
}
);

});

package com.goodtaste.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.goodtaste.constantfiled.ConstantFiled;
import com.goodtaste.model.User;
import com.goodtaste.service.IUserService;
import com.goodtaste.util.ResponseData;
@Controller
public class UserController extends BaseController {
@Resource(name = "UserService")
private IUserService userService;
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
@RequestMapping("/RegisterUser.do")
public void registerUser(
@RequestParam(value = ConstantFiled.USERNAME) String userName,
@RequestParam(value = ConstantFiled.PASSWORD) String passWord,
@RequestParam(value = ConstantFiled.GROUPID) int groupId,
HttpServletResponse response) {
try {
//操作用户。。。
User user = new User();
user.setUserName(userName);
&n
3ff8
bsp; user.setPassWord(passWord);
user.setGroupId(groupId);
boolean result = this.getUserService().saveUser(user);
if (result) {
ResponseData.handleReauestData(true, this.getMap(),
this.getData(), this.getObjectMapper(), response);
} else {
ResponseData.handleReauestData(false, this.getMap(),
this.getData(), this.getObjectMapper(), response);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}


其他的代码就不再累述了!!

下面输入链接:http://localhost:8080/goodtaste_system/registerUser.jsp



数据库:

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