1、在eclipse中安装Gradle插件
2、新建Gradle Project
方法一:直接新建
方法二:
1) 直接新建一个Dynamic Web Project
2) 右击项目—>Configure—>Convert to Gradle Prorject
3、转换项目之后,在src下如下目录,其中java放置源代码,resources放置配置文件,webapp放置视图资源和web.xml。
3、 在项目根目录下新建build.gradle,下载依赖包
1)build.gradle文件
apply plugin: 'java'
apply plugin: 'war' //用来生成war
apply plugin: 'eclipse-wtp' //用来生成Eclipseweb项目的插件(web-tool-platform)
version = '1.0' //property
// Uses JDK 7
sourceCompatibility = 1.7
targetCompatibility = 1.7
// 1. Get dependencies from Maven local repository
// 2. Get dependencies from Maven central repository
repositories {
mavenCentral()
}
//Project dependencies
dependencies {
compile 'org.apache.tomcat:tomcat-servlet-api:8.0.24'
compile 'jstl:jstl:1.2'
compile 'org.springframework:spring-beans:4.1.7.RELEASE'
compile 'org.springframework:spring-web:4.1.7.RELEASE'
compile 'org.springframework:spring-webmvc:4.1.7.RELEASE'
compile 'org.springframework:spring-tx:4.1.7.RELEASE'
compile 'com.alibaba:druid:1.0.15'
compile 'org.aspectj:aspectjweaver:1.8.6'
compile 'mysql:mysql-connector-java:5.1.36'
compile 'org.mybatis:mybatis-spring:1.2.3'
compile 'org.mybatis:mybatis:3.3.0'
compile 'org.springframework:spring-jdbc:4.1.7.RELEASE'
compile 'junit:junit:4.12'
compile 'org.springframework:spring-test:4.0.5.RELEASE'
//include in compile only, exclude in the war
providedCompile 'javax.servlet:servlet-api:2.5'
}
在build.gradle文件中配置我们项目需要依赖的包,这些我们使用的是maven仓库中的资源。关于这些jar包在哪里找的,这里推荐一个网站: http://www.mvnrepository.com/
2)下载依赖包
右击项目—>Gradle—>Refresh Dependencies,等待依赖下载完成。
4、整合Spring-Mybatis
1)配置文件
spring-mybatis.xml配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
jdbc.propreties配置文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmdemo
username=root
password=root
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
2)开发dao层和service类
UserMapper.java
public interface UserMapper {
public User selectByPrimaryKey(int userid);
public int saveUser(User user);
public User userLogin(User user);
}
User.java
public class User {
private int id;
private String userName;
private String password;
private int age;
//...getter
//....setter
}
UserMapper.xml
select * from user where id = #{id} insert into user (username,password,age) value (#{userName},#{password},#{age}) select * from user where username=#{userName} and password=#{password}
UserService.java
public interface UserService {
public User getUserById(int userId);
public int register(User user);
public User login(User user);
}
UserService.java
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(int userId) {
return userMapper.selectByPrimaryKey(userId);
}
@Override
public int register(User user) {
return userMapper.saveUser(user);
}
@Override
public User login(User user) {
return userMapper.userLogin(user);
}
}
3)开发测试类
TestMyBatis
.java
@RunWith(value = SpringJUnit4ClassRunner.class) //测试运行器
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) //加载配置文件
public class TestMyBatis {
@Autowired
private UserService userService;
@Test
public void test(){
System.err.println(userService.getUserById(1).getUserName());
}
}
5、整合Spring MVC
1)配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
2)开发控制类
UserController.java
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
return "index";
}
@RequestMapping(value = "/register.do", method = RequestMethod.POST)
public String register(User user, Model model) {
if (null == user.getUserName() || "".equals(user.getUserName())) {
model.addAttribute("message", "用户名不能为空!!");
return "error";
}
if (null == user.getPassword() || "".equals(user.getPassword())) {
model.addAttribute("message", "密码不能为空!!");
return "error";
}
if (user.getAge() < 18) {
model.addAttribute("message", "用户年龄不满18!!");
return "error";
}
if (1 == userService.register(user)) {
model.addAttribute("user", user);
model.addAttribute("message", "恭喜注册成功!!");
return "success";
}
model.addAttribute("message", "未知错误!");
return "error";
}
@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public String login(User user, Model model) {
if (null == user.getUserName() || "".equals(user.getUserName())) {
model.addAttribute("message", "用户名不能为空!!");
return "error";
}
if (null == user.getPassword() || "".equals(user.getPassword())) {
model.addAttribute("message", "密码不能为空!!");
return "error";
}
user = userService.login(user);
if (user != null && user.getId() > 0) {
model.addAttribute("user", user);
model.addAttribute("message", "登录成功!!");
return "success";
}
model.addAttribute("message", "未知错误!");
return "error";
}
}
3)在web.xml中整合三大框架
web.xml
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_3_0.xsd" id="WebApp_ID" version="3.0"> contextConfigLocation classpath:spring-mybatis.xml encodingFilter org.springframework.web.filter.CharacterEncodingFilter true encoding UTF-8 encodingFilter /* org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 true SpringMVC *.do /index.jsp
4)新建视图资源
5、运行测试
(2)
本文参考:http://blog.csdn.net/gebitan505/article/details/44455235(使用maven构建SSM)