Spring
发布时间:2019-10-07 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJS整合Springmvc、Spring、Mybatis搭建开发环境,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
最近想学习AngularJS的使用,网上搜了一圈后,折腾了半天解决bug后,成功使用AngularJS整合Springmvc、Spring、Mybatis搭建了一个开发环境。(这里Spring使用的版本是4.0.6,Mybatis版本是3.2.5,AngularJS的版本是1.0.3)
第一步:
创建一Maven项目,在po
m.xml下添加需要的包
ject xmlns="
http://maven.apache
.org/POM/4.0.0" xmlns:xsi="
http://www.w3
.org/2001/XML
scheR_886_11845@a-instance"
xsi:
scheR_886_11845@a
LOCATIOn="
http://maven.apache
.org/POM/4.0.0
http://maven.apache
.org/maven-v4_0_0.xsd">
4.0.0
test.AngularSpringmvcMybatis
@H_
618_10@AngularSpringmvcMybatis
war
0.0.1-SNAPSHOT
AngularSpringmvcMybatis Maven Webapp
http://maven.apache.org
junit
@H_618_10@junit
3.8.1
test
org.springframework
@H_618_10@spring-webmvc
4.0.6.RELEASE
org.springframework
@H_618_10@spring-core
4.0.6.RELEASE
org.springframework
@H_618_10@spring-tx
4.0.6.RELEASE
org.springframework
@H_618_10@spring-jdbc
4.0.6.RELEASE
org.springframework
@H_618_10@spring-orm
4.0.6.RELEASE
org.springframework
@H_618_10@spring-aspects
4.0.6.RELEASE
org.springframework
@H_618_10@spring-context-support
4.0.6.RELEASE
org.mybatis
@H_618_10@mybatis
3.2.5
org.mybatis
@H_618_10@mybatis-spring
1.2.0
org.aspectj
@H_618_10@aspectjweaver
1.6.8
@H_737_8@mysql
@H_618_10@mysql-connector-java
5.1.6
c3p0
@H_618_10@c3p0
0.9.1
log4j
@H_618_10@log4j
1.2.16
javax.servlet
@H_618_10@servlet-api
3.0-alpha-1
asm
@H_618_10@asm
3.3
asm
@H_618_10@asm-commons
3.3
asm
@H_618_10@asm-tree
3.3
ognl
@H_618_10@ognl
3.0.6
commons-logging
@H_618_10@commons-logging
1.1.3
org.apache.velocity
@H_618_10@velocity
1.7
org.codehaus.jackson
@H_618_10@jackson-mapper-asl
1.9.12
@H_197_199@
AngularSpringmvcMybatis
第二步:
在src/main/
resources下添加配置文件,如下:
(注:如果刚创建的maven项目中没显示src/main/resources与src/test/java目录,可以右键项目,再properties,在Java Build Path中,将JRE System Library修改为1.7版本,如下)
配置文件中applicationContext.xml如下:
http://www.springframework.org/scheR_886_11845@a/beans"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_886_11845@a-instance"
xmlns:p="http://www.springframework.org/scheR_886_11845@a/p"
xmlns:aop="http://www.springframework.org/scheR_886_11845@a/aop"
xmlns:context="http://www.springframework.org/scheR_886_11845@a/context"
xmlns:jee="http://www.springframework.org/scheR_886_11845@a/jee"
xmlns:tx="http://www.springframework.org/scheR_886_11845@a/tx"
xsi:scheR_886_11845@aLOCATIOn="
http://www.springframework.org/scheR_886_11845@a/aop http://www.springframework.org/scheR_886_11845@a/aop/spring-aop-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/beans http://www.springframework.org/scheR_886_11845@a/beans/spring-beans-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/context http://www.springframework.org/scheR_886_11845@a/context/spring-context-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/jee http://www.springframework.org/scheR_886_11845@a/jee/spring-jee-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/tx http://www.springframework.org/scheR_886_11845@a/tx/spring-tx-4.0.xsd">
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriveRMANagerDatasource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_news"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
LOCATIOns" value="classpath:com/hin/mappers/*.xml">
LOCATIOn" value="classpath:mybatis-config.xml">
<bean class="org.mybatis.spring.mapper.MapperScAnnerConfigurer">
Anname" value="sqlSessionFactory">
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DatasourcetransactionManager">
<tx:advice id="txAdvice" transaction-manager="transactionManager">
spring-mvc.xml如下:
http://www.springframework.org/scheR_886_11845@a/beans"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_886_11845@a-instance"
xmlns:p="http://www.springframework.org/scheR_886_11845@a/p"
xmlns:aop="http://www.springframework.org/scheR_886_11845@a/aop"
xmlns:mvc="http://www.springframework.org/scheR_886_11845@a/mvc"
xmlns:context="http://www.springframework.org/scheR_886_11845@a/context"
xmlns:jee="http://www.springframework.org/scheR_886_11845@a/jee"
xmlns:tx="http://www.springframework.org/scheR_886_11845@a/tx"
xsi:scheR_886_11845@aLOCATIOn="
http://www.springframework.org/scheR_886_11845@a/aop http://www.springframework.org/scheR_886_11845@a/aop/spring-aop-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/beans http://www.springframework.org/scheR_886_11845@a/beans/spring-beans-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/mvc http://www.springframework.org/scheR_886_11845@a/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/context http://www.springframework.org/scheR_886_11845@a/context/spring-context-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/jee http://www.springframework.org/scheR_886_11845@a/jee/spring-jee-4.0.xsd
http://www.springframework.org/scheR_886_11845@a/tx http://www.springframework.org/scheR_886_11845@a/tx/spring-tx-4.0.xsd">
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/html/"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".html"/>
<property name="exposeSpringMacroHelpers" value="true"/>
完后配置web.xml,如下:
Archetype Created Web Application
contextConfigLOCATIOn
classpath:applicationContext.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLOCATIOn
classpath:spring-mvc.xml
1
springMVC
/
第三步:
编写各个Java类,以下是用户控制器(实现db_news数据库中t_user表的用户添加与用户删除)
m.hin.controller;
import java.uti
l.List;
import javax.
Annotation.
resource;
import org.springframework.stereotype.Controller;
import org.springframework.web
.bind.
Annotation.PathVariable;
import org.springframework.web
.bind.
Annotation.@R_
262_10613@estMapping;
import org.springframework.web
.bind.
Annotation.@R_
262_10613@estMethod;
import org.springframework.web
.bind.
Annotation.ResponseBody;
import co
m.hin.entity.User;
import co
m.hin.
service.User
service;
@Controller
@@R_262_10613@estMapping("/users")
public class UserController {
@resource
private Userservice userservice;
@@R_262_10613@estMapping("/userlist.json")
public @ResponseBody List getUserList() {
return userservice.getAllUsers();
}
@@R_262_10613@estMapping(value = "/addUser/{userNamE}",method = @R_262_10613@estMethod.POST)
public @ResponseBody void addUser(@PathVariable("userName") String userName) {
userservice.addUser(userName);
}
@@R_262_10613@estMapping(value = "/removeUser/{userNamE}",method = @R_262_10613@estMethod.Delete)
public @ResponseBody void removeUser(@PathVariable("userName") String userName) {
userservice.deleteUser(userName);
}
@@R_262_10613@estMapping(value = "/removeAllUsers",method = @R_262_10613@estMethod.Delete)
public @ResponseBody void removeAllUsers() {
userservice.deleteAll();
}
@@R_262_10613@estMapping("/layout")
public String getUserPartialPage() {
return "users/layout";
}
}
第四步:
引入angular的js文件,如下:
这里使用Angular来实现添加用户与删除用户功能主要是UserController.js,如下:
Strict';
/**
- UserController
*/
var UserController = function($scope,$http) {
$scope.fetchUsersList = function() {
$http.get('users/userlist.json').success(function(userList){
$scope.users = userList;
});
};
$scope.addNewUser = function(newUser) {
$http.post('users/addUser/' + newUser).success(function() {
$scope.fetchUsersList();
});
$scope.userName = '';
};
$scope.removeUser = function(user) {
$http.delete('users/removeUser/' + user).success(function() {
$scope.fetchUsersList();
});
};
$scope.removeAllUsers = function() {
$http.delete('users/removeAllUsers').success(function() {
$scope.fetchUsersList();
});
};
$scope.fetchUsersList();
};
关于Angular的其他文件具体可看源码,最后再右键项目,Run as,Maven install,再发布到tomcat下就可以看到效果了,如下:
AngularJS整合Springmvc、Spring、Mybatis搭建开发环境就完成了,希望对大家的学习有所帮助。
大佬总结
以上是大佬教程为你收集整理的AngularJS整合Springmvc、Spring、Mybatis搭建开发环境全部内容,希望文章能够帮你解决AngularJS整合Springmvc、Spring、Mybatis搭建开发环境所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。