linux怎么查看本机内存大小
192
2023-07-14
使用idea2017搭建SSM框架(图文步骤)
搭建个SSM框架居然花费了我好长时间!特此记录!
需要准备的环境:
idea 2017.1
jdk1.8
Maven 3.3.9
请提前将idea与Maven、jdk配置好,本次项目用的都是比较新的
注:配置完ide红线报错没关系!可以run!
步骤:
一、首先使用idea新建一个Maven webapp项目
点击Finish,第一次搭建可能会很慢,甚至可能需要VPN才能搭建成功
二、搭建目录结构
我这里列出的是搭建完了之后所有的目录和文件,诸位先把目录文件建起来,然后我在给出文件内容
这里的目录建好之后还需要设置一下,让idea识别目录作用,选择File-Project Structure
设置完成后ok
三、配置文件内容
pom.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
注意右下角更新pom
logback.xml
这里可以控制输出格式和内容,有兴趣的可以自己设置
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://xxxxxxxxx:3306/ChatRobot?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=xxxx
#密码
jdbc.password=xxxxx
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2
spring-mybatis.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd">
spring-mvc.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">
web.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
到这里基本上环境就搭建完成了,下面开始测试
四、测试
先导入一份数据库测试文件
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', 'xxx', 'xxxxx', 'xxxxx', 'root', '0', '2017-03-28 09:40:31', '127.0.0.1');
SET FOREIGN_KEY_CHECKS=1;
接下来配置类
UserController
package com.chatRobot.controller;
import javax.servlet.http.HttpServletRequest;
import com.chatRobot.model.User;
import com.chatRobot.service.IUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
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();
}
}
IUserDao
package com.chatRobot.dao;
import com.chatRobot.model.User;
public interface IUserDao {
User selectUser(long id);
}
User
package com.chatRobot.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(int 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;
}
}
IUserService
package com.chatRobot.service;
import com.chatRobot.model.User;
public interface IUserService {
public User selectUser(long userId);
}
UserServiceImpl
package com.chatRobot.service.impl;
import com.chatRobot.dao.IUserDao;
import com.chatRobot.model.User;
import com.chatRobot.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);
}
}
UserDao.xml
SELECT * FROM user WHERE id = #{id}
然后新建个测试类,来测试mybatis
IUserDaoTest
package com.chatRobot.dao;
import com.chatRobot.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());
}
}
运行后结果应该是会在控制台输出id为1的用户名,没成功的话就找BUG去吧...
继续新建个页面来测试springmvc和mybatis
index.html
function selectUser() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("test").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", http://"user/showUser.do", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-formkPdLvIeiD-urlencoded");
xmlhttp.send("id=1");
}
Hello World!
新建完成后配置项目运行环境,点击Run-Edit Configurations...
点击加号新建运行环境,选择Tomcat Server-Local
选中新建好的服务器,右边选择Deployment,点击加号-Atifact...
选择第二项
然后在右边Application context配置你的项目名
最后运行项目,在打开的页面中点击按钮测试,成功的话会在页面上显示id为1的用户信息
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~