博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
idea搭建简易ssm项目
阅读量:5291 次
发布时间:2019-06-14

本文共 10607 字,大约阅读时间需要 35 分钟。

按照来搭建ssm框架,一步成功,特此记录一下。

本机环境:

  • idea2017.3
  • jdk1.8
  • mysql5.5
  • maven3.3.9

步骤:

1. idea新建maven webapp项目

2. 搭建项目结构

按照该目录结构创建好相应的文件

搭建时有两个点需要注意一下(针对idea新手):

(1)新建的目录用途需要设置一下,否则新建时无class文件的选项

(2)有的文件类型没有配置(如xml),所以需要在idea中配置一下模板

3. 配置配置文件内容

pom.xml

4.0.0
com.zang
userManager
1.0-SNAPSHOT
war
userManager Maven Webapp
http://www.example.com
UTF-8
UTF-8
4.3.5.RELEASE
3.4.1
javax
javaee-api
7.0
junit
junit
4.12
ch.qos.logback
logback-classic
1.2.2
com.fasterxml.jackson.core
jackson-databind
2.8.7
mysql
mysql-connector-java
5.1.41
runtime
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.1
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
ChatRobot
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver#数据库地址jdbc.url=jdbc:mysql://127.0.0.1:3306/solr?useUnicode=true&characterEncoding=utf8#用户名jdbc.username=root#密码jdbc.password=123#最大连接数c3p0.maxPoolSize=30#最小连接数c3p0.minPoolSize=10#关闭连接后不自动commitc3p0.autoCommitOnClose=false#获取连接超时时间c3p0.checkoutTimeout=10000#当获取连接失败重试次数c3p0.acquireRetryAttempts=2

logback.xml

%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n

spring-mvc.xml

spring-mybatis.xml

web.xml

ChatRobot
ChatRobot_Alpha_0.0.1
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-*.xml
1
true
SpringMVC
/
index.html

4. 增加类相关代码

导入数据库测试文件

CREATE TABLE `user` (  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',  `email` varchar(255) NOT NULL COMMENT '用户邮箱',  `password` varchar(255) NOT NULL COMMENT '用户密码',  `username` varchar(255) NOT NULL COMMENT '用户昵称',  `role` varchar(255) NOT NULL COMMENT '用户身份',  `status` int(1) NOT NULL COMMENT '用户状态',  `regTime` datetime NOT NULL COMMENT '注册时间',  `regIp` varchar(255) NOT NULL COMMENT '注册IP',  PRIMARY KEY (`id`),  UNIQUE KEY `email` (`email`) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;INSERT INTO `user` VALUES ('1', '222@qq.com', '123456', '大明', 'root', '0', '2017-03-28 09:40:31', '127.0.0.1');SET FOREIGN_KEY_CHECKS=1;

com.zang.model.User

package com.zang.model;import java.util.Date;public class User {    private long id;    private String email;    private String password;    private String username;    private String role;    private int status;    private Date regTime;    private String regIp;    public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getRole() {        return role;    }    public void setRole(String role) {        this.role = role;    }    public int getStatus() {        return status;    }    public void setStatus(int status) {        this.status = status;    }    public Date getRegTime() {        return regTime;    }    public void setRegTime(Date regTime) {        this.regTime = regTime;    }    public String getRegIp() {        return regIp;    }    public void setRegIp(String regIp) {        this.regIp = regIp;    }}

com.zang.controller.UserController

package com.zang.controller;import com.fasterxml.jackson.databind.ObjectMapper;import com.zang.model.User;import com.zang.service.IUserService;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@Controller@RequestMapping("/user")public class UserController {    @Resource    private IUserService userService;    @RequestMapping("/showUser.do")    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {        request.setCharacterEncoding("UTF-8");        response.setCharacterEncoding("UTF-8");        long userId = Long.parseLong(request.getParameter("id"));        User user = this.userService.selectUser(userId);        ObjectMapper mapper = new ObjectMapper();        response.getWriter().write(mapper.writeValueAsString(user));        response.getWriter().close();    }}

com.zang.service.IUserService

package com.zang.service;import com.zang.model.User;public interface IUserService {    public User selectUser(long userId);}

com.zang.service.impl.UserServiceImpl

package com.zang.service.impl;import com.zang.dao.IUserDao;import com.zang.model.User;import com.zang.service.IUserService;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Service("userService")public class UserServiceImpl implements IUserService{    @Resource    private IUserDao userDao;    public User selectUser(long userId) {        return this.userDao.selectUser(userId);    }}

com.zang.dao.IUserDao

package com.zang.dao;import com.zang.model.User;public interface IUserDao {    User selectUser(long id);}

mapper/UserDao.xml

5. 测试

新建测试类进行测试

IUserDaoTest

package com.zang.dao;import com.zang.model.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;// 加载spring配置文件@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({
"classpath:spring-mybatis.xml"})public class IUserDaoTest { @Autowired private IUserDao dao; @Test public void testSelectUser() throws Exception { long id = 1; User user = dao.selectUser(id); System.out.println(user.getUsername()); }}

test

通过web页面发布到服务器,测试springmvc 和 mybatis

index.html

    
test

Hello World!

配置项目运行环境,点击Run-Edit Configurations...

添加本地tomcat

启动tomcat

点击测试,显示用户信息

 

转载于:https://www.cnblogs.com/zjfjava/p/9222355.html

你可能感兴趣的文章
static使用方法小结
查看>>
Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)
查看>>
Quick Tip: How to Add Syntax Highlighting to Any Project
查看>>
BoundsChecker使用
查看>>
深度学习框架Keras
查看>>
十大经典误会
查看>>
(C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单
查看>>
电子书下载:Test Drive ASP.NET MVC
查看>>
DirectInput里的键盘鼠标的应用
查看>>
ASP.NET MVC 拓展ActionResult实现Html To Pdf 导出
查看>>
JavaScript实现依赖注入
查看>>
喝酒游戏,概率分布和卷积
查看>>
用matlab查找txt文档中的关键字,并把关键字后面的数据存到起来用matlab处理
查看>>
Grow A Search Result Specification Update
查看>>
《CSS3秘籍》(第三版)-读书笔记(2)
查看>>
Chrome使用记录
查看>>
[LeetCode] Move Zeroes
查看>>
SharePoint添加列表
查看>>
update 两表更新(mysql)
查看>>
C# Math类详细说明
查看>>