java中常用前后端交互框架,整合Spring-SpringMVC-MyBatis实现简单前后端交互

整合Spring-SpringMVC-MyBatis实现简单前后端交互

(1)环境要求

IDEA

MySQL

Tomcat

Maven

(2)数据库环境

id name pwd

1 Hunter 12

2 Nexa 34

3 Kennys 56

4 Niko 78

(3)SSM基本环境搭建

1.新建Maven项目,项目添加Web app支持

2.在pom.xml中导入相关依赖

junit

junit

4.12

test

mysql

mysql-connector-java

8.0.22

com.mchange

c3p0

0.9.5.2

javax.servlet

servlet-api

2.5

provided

javax.servlet.jsp

jsp-api

2.2

provided

javax.servlet

jstl

1.2

org.mybatis

mybatis

3.5.6

org.mybatis

mybatis-spring

2.0.5

org.springframework

spring-webmvc

5.2.10.RELEASE

org.springframework

spring-jdbc

5.2.10.RELEASE

org.projectlombok

lombok

1.18.12

provided

3.Maven资源过滤问题设置

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

4.搭建基本结构和框架配置

org.hac.controller

org.hac.pojo

org.hac.dao

org.hac.service

applicationContext.xml(Spring核心配置文件)

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

(4)Mybatis层编写

1.数据库配置文件 database.properties

jdbc.driver=com.mysql.cj.jdbc.Driver

#jdbc.serverTimezone=UTC

jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai

jdbc.username=root

jdbc.password=hac0207

2.IDEA关联数据库

关联数据库

3.编写mybatis-config.xml

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

4.编写具体的接口、类、Mapper配置文件(分Dao层和Servcie层)

1)编写数据库对应的实体类

@Data

@AllArgsConstructor

@NoArgsConstructor

public class User {

private int id;

private String name;

private String pwd;

}

2)定义dao层的Mapper接口

public interface UserMapper {

void addUser(User user);

void deleteUser(int id);

void updateUser(User user);

List selectUser();

User selectUserById(int id);

}

3)编写接口对应的Mapper.xml文件

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

insert into user (id,name,pwd) values (#{id},#{name},#{pwd})

delete from user where id=#{id}

update user set name=#{name},pwd=#{pwd} where id=#{id}

select * from user

select * from user where id=#{id};

4)编写Service层的接口和实现类

public interface UserService {

void addUser(User user);

void deleteUser(int id);

void updateUser(User user);

List selectUser();

User selectUserById(int id);

}

public class UserServiceImpl implements UserService {

private UserMapper userMapper;

public void setUserMapper(UserMapper userMapper) {

this.userMapper = userMapper;

}

public void addUser(User user) {

userMapper.addUser(user);

}

public void deleteUser(int id) {

userMapper.deleteUser(id);

}

public void updateUser(User user) {

userMapper.updateUser(user);

}

public List selectUser() {

return userMapper.selectUser();

}

public User selectUserById(int id) {

return userMapper.selectUserById(id);

}

}

(5)Spring层编写

1.配置Spring整合Mybatis,数据源使用c3p0连接池;

2.编写Spring整合Mybatis的相关的配置文件:Spring-dao.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

3.Spring整合Service层

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

(6)SpringMVC层编写

Spring配置中说明

在XML中配置了这个标签后,spring可以自动扫描base-package下面或者子包下面的java文件,如果扫描有@Component @Service @Controller等这些注解的类,则把这些类注册为bean。

1.web.xml配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

DispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

DispatcherServlet

/

endcodingFilter

org.springframework.web.filter.CharacterEncodingFilter

endcoding

utf-8

endcodingFilter

/*

15

2.spring-mvc.xml配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

3.Spring配置整合文件:applicationContext.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

(7)Controller编写

1.UserController编写

@Controller

@RequestMapping("/user")

public class UserController {

@Autowired

@Qualifier("userServiceImpl")

private UserServiceImpl userService;

@RequestMapping("/selectUser")

public String selectUser(Model model){

List users=userService.selectUser();

model.addAttribute("msg",users);

return "user";

}

}

(8)起始页和跳转页编写

1.编写首页index.jsp

首页

这是首页

2.检索成员的跳转页user.jsp

User

${msg}

(9)配置Tomcat测试

起始页

跳转页

(10)问题排查

1.资源过滤问题

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

2.需要检查target目录中是否缺少配置文件,必要时手动添加

3.运行Tomcat前检查项目设置中的Artifacts中是否导入了相关的jar包

4.MySQL8.0以上版本中,配置文件database.properties中设置应多"jc"和注意时区设置

jdbc.driver=com.mysql.cj.jdbc.Driver

5.spring-dao配置文件中关联数据库配置文件中路径设置要加上classpath:

(11)SSM搭建思维导图

项目目录

思维导图

以上;

懂了吗,那就去用SpringBoot吧doge~~

你可能感兴趣的:(java中常用前后端交互框架)