多平台统一管理软件接口,如何实现多平台统一管理软件接口
241
2023-01-07
spring+springmvc+mybatis+maven入门实战(超详细教程)
入门篇
本篇文章涉及到的技术有spring、springmvc、mybatis、mysql、xml、maven、jsp、javase、javaweb、eclipse
下面开始本篇文章的教程
一、新建maven项目
一般通过这种方法新建maven项目
假如你的eclipse不能通过上面的方法新建maven项目,也可以通过下面的方法新建maven项目
看到下面的项目结构,说明你已经成功创建了一个maven项目。但是这个项目报错,根据标准的web项目结构来说,目前这个项目缺少了web.xml
利用eclipse自动生成web.xml
看到这个项目已经没有报错,说明已经成功生成了web.xml
二、Maven配置
下面是该项目的maven配置,在pom.xml进行以下配置
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> http://repository.apache.org/content/groups/snapshots-group/
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
http://repository.apache.org/content/groups/snapshots-group/
三、搭建ssm环境
1.在src/main/java新建4个包、在src/main/resources下新建2个包以及2个properties文件、在webapp/WEB-INF下新建1个文件夹,包名、文件名以及文件夹名如图所示。
src/main/java:一般用来存放java文件
src/main/resources:一般用来存放ssm相关的配置文件
src/test/java和src/test/resources:一般用来存放测试文件,这两个包下的文件不会被编译、打包等
webapp/WEB-INF:一般用来存放普通用户访问不到而管理员可以访问的web动态页面
2.配置log4j日志文件
log4j.properties代码如下,有些网友在复制楼主的代码会产生乱码,这是由于eclipse的properties文件默认编码为ISO-8859-1
我们需要将编码设置为UTF-8
##### Global logging configuration
##### 在开发环境下日志级别设置成DEBUG,生产环境设置成INFO或ERROR
log4j.rootLogger=DEBUG, stdout
##### mybatis日志配置
log4j.logger.org.mybatis.example.BlogMapper=TRACE
##### 输出到控制台,这里也可以输出到文件中,可以自己设置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3.配置数据源文件
db.properties代码如下,此处采用非硬编码的方式对数据源进行配置,也就是将数据源相关信息配置到db.properties中,采用非硬编码的好处是方便项目日后的维护以及方便拓展。当然你也可是采用硬编码方式(传统方式)进行配置,也就是直接在spring配置文件中配置。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=test
4.在mysql中新建ssm数据库,并在ssm数据库中新建一个user表,表的结构如图所示
5.com.wffanshao.po中新建一个PO类,名字为User
User.java的代码如下
package com.wffanshao.po;
/**
* @描述 用户PO类
* @author WF帆少
* @微信 13025261795
*
*/
public class User {
private String username; // 用户名
private String password; // 用户密码
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6.在com.wffanshao.mapper中新建UserMapper接口和UserMapper.xml
UserMapper.java代码如下
package com.wffanshao.mapper;
import com.wffanshao.po.User;
/**
* @描述 用户Mapper接口
* @author WF帆少
* @微信 13025261795
*
*/
public interface UserMapper {
/**
* @描述 添加用户
* @param user
* @throws Exception
*/
boolean insertUser(User user) throws Exception;
}
UserMapper.xml代码如下
INSERT INTO user(username, password)
VALUES(#{username}, #{password})
7.在com.wffanshao.service中新建UserService接口
UserService.java代码如下
package com.wffanshao.service;
import com.wffanshao.po.User;
/**
* @描述 用户Service接口
* @author WF帆少
* @微信 13025261795
*
*/
public interface UserService {
/**
* @描述 添加用户
* @param user
* @throws Exception
*/
boolean insertUser(User user) throws Exception;
}
8.在com.wffanshao.service.impl中新建UserServiceImpl类
UserServiceImpl.java代码如下
package com.wffanshao.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.wffanshao.mapper.UserMapper;
import com.wffanshao.po.User;
import com.wffanshao.service.UserService;
/**
* @描述 用户Service接口的实现类
* @author WF帆少
* @微信 13025261795
*
*/
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
/**
* @描述 添加用户
* @param user
* @throws Exception
*/
@Override
public boolean insertUser(User user) throws Exception {
return userMapper.insertUser(user);
}
}
9..在com.wffanshao.controller中新建UserController类
UserController.java代码如下
package com.wffanshao.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.wffanshao.po.User;
import com.wffanshao.service.UserService;
/**
* @描述 用户Controller
* @author WF帆少
* @微信 13025261795
*
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
/**
* @描述 跳转到login.jsp
* @return
*/
@GetMapping("/login")
public String login() {
return "login";
}
/**
* @描述 从login.jsp中的表单提交中获取数据并将它们添加到数据库中,
* @return 如果添加成功,跳转到success.jsp,否则,跳转到fail.jsp
*/
@PostMapping("/insertUser")
public String insertUser(HttpServletRequest request) throws Exception {
User user = new User();
String username = request.getParameter("username");
String password = request.getParameter("password");
user.setUsername(username);
user.setPassword(password);
boolean isSuccess = false;
isSuccess = userService.insertUser(user);
if (isSuccess) {
return "success";HkNpO
} else {
return "fail";
}
}
}
10.在webapp/WEB-INF/jsp下新建login.jsp、success.jsp、fail.jsp
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
11.在src/main/resources/spring中新建4个xml文件
application-dao.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" 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/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
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/mvc
http://springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.2.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.2.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.2.xsd">
application-service.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" 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/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
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/mvc
http://springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.2.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.2.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.2.xsd">
applicationContext-transaction.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.orghttp:///schema/context" xmlns:aop="http://springframework.org/schema/aop" 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/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:context="http://springframework.orghttp:///schema/context"
xmlns:aop="http://springframework.org/schema/aop"
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/mvc
http://springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.2.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.2.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.2.xsd">
springmvc.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" 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/mvc http://springframework.org/schema/mvc/spring-mvc-3.2.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.2.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.2.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.2.xsd"> base-package="com.wffanshao.controller"> class="org.springframework.web.servlet.view.InternalResourceViewResolver">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
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/mvc
http://springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context-3.2.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.2.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx-3.2.xsd">
base-package="com.wffanshao.controller">
base-package="com.wffanshao.controller">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
四、配置tomcat服务器
注意:博主使用的是tomcat7
这里没有教大家如何下载安装配置tomcat服务器,请大家自行学习,或者等博主后续教程补上。这里只教大家如何用eclipse配置tomcat服务器,需要提前下载解压安装好tomcat才能进行此步操作
配置好tomcat服务器之后,你会发现项目报错了
打开查看报错原因,你会发现该项目是因为无法解析jsp而报错,我们可以通过设置项目属性来解决该问题,即设置该项目的运行环境
设置完成后你会发项目已经不报错了
四、装配项目
将该项目添加到tomcat服务器中
到这里,已经成功将项目装配到tomcat服务器中了。
五、启动tomcat服务器
启动服务器
看到started并且控制台没有报错说明服务器启动成功
六、测试
在浏览器地址栏中http://localhost:8080/test/login,tomcat服务器默认端口为8080
接着输入用户名和密码,然后点击注册
注册成功会跳转到成功页面,并且在数据库中能查询到我们刚才输入的用户名和密码,
看到下面2张图所示的内容,证明ssm环境整合成功
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~