使用IDEA搭建ssm框架的详细图文教程

网友投稿 210 2022-11-23


使用IDEA搭建ssm框架的详细图文教程

ssm(spring springMVC mybatis)

1.创建项目

file->new->project

2.新建的maven项目目录结构

添加ssm需要的文件夹等

如果去掉java文件夹的蓝色标志,会发现这里new时不能创建java类或包

如果main/java前不是蓝色文件夹或test/java前不是绿色文件夹,可以这样添加

3.加入maven依赖

pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

ssm3

1.0-SNAPSHOT

war

ssm3 Maven Webapp

http://example.com

UTF-8

1.7

1.7

2.5.4

<dependency>

com.fasterxml.jackson.core

jackson-core

${jackson.version}

com.fasterxml.jackson.core

jackson-databind

${jackson.version}

org.springframework

spring-webmvc

4.3.7.RELEASE

org.springframework

spring-jdbc

4.3.7.RELEASE

org.springframework

spring-aspects

4.3.7.RELEASE

org.mybatis

mybatis

3.4.2

org.mybatis

mybatis-spring

1.3.1

c3p0

c3p0

0.9.1.2

mysql

mysql-connector-java

8.0.13

jstl

jstl

1.2

javax.servlet

javax.servlet-api

3.0.1

provided

org.mybatis.generator

mybatis-generator-core

1.3.5

com.github.pagehelper

pagehelper

5.0.0

ssm3

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

ssm3

1.0-SNAPSHOT

war

ssm3 Maven Webapp

http://example.com

UTF-8

1.7

1.7

2.5.4

<dependency>

com.fasterxml.jackson.core

jackson-core

${jackson.version}

com.fasterxml.jackson.core

jackson-databind

${jackson.version}

org.springframework

spring-webmvc

4.3.7.RELEASE

org.springframework

spring-jdbc

4.3.7.RELEASE

org.springframework

spring-aspects

4.3.7.RELEASE

org.mybatis

mybatis

3.4.2

org.mybatis

mybatis-spring

1.3.1

c3p0

c3p0

0.9.1.2

mysql

mysql-connector-java

8.0.13

jstl

jstl

1.2

javax.servlet

javax.servlet-api

3.0.1

provided

org.mybatis.generator

mybatis-generator-core

1.3.5

com.github.pagehelper

pagehelper

5.0.0

ssm3

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

4.加入spring配置文件(applicationContext.xml)

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

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/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.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: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/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd">

5.配置数据库连接相关信息(dbconfig.properties)

#数据库连接

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm-crud?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8

#加载驱动

jdbc.driverClass=com.mysql.cj.jdbc.Driver

#数据库登录用户名

jdbc.user=root

#密码

jdbc.password=root

6.mybatis配置文件(mybatis-config.xml)

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

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

注意:这里要建个mapper空文件,因为spring的配置文件applicationContext.xml

中有一项配置要扫描这个文件夹下的所有映射文件

7.配置web.xml文件

web.xml

项目启动,先加载web.xml文件的配置

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

1

dispatcherServlet

/

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

characterEncodingFilter

/*

8.配置spring mvc配置文件(dispatcherServlet-servlet.xml)

spring mvc就是充当控制器,替代servlet

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.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.xsd">

9.使用逆向工程前配置

PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

connectionURL="jdbc:mysql://localhost:3306/ssm-crud?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8"

userId="root"

password="root">

connectionURL="jdbc:mysql://localhost:3306/ssm-crud?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8"

userId="root"

password="root">

targetProject=".\src\main\resources">

targetProject=".\src\main\resources">

targetaTXElXfEPackage="com.ssm.dao"

targetProject=".\src\main\java">

targetaTXElXfEPackage="com.ssm.dao"

targetProject=".\src\main\java">

10.使用逆向工程生成接口、实体类、映射文件

MBGTest.java

import org.mybatis.generator.api.MyBatisGenerator;

import org.mybatis.generator.config.Configuration;

import org.mybatis.generator.config.xml.ConfigurationParser;

import org.mybatis.generator.exception.InvalidConfigurationException;

import org.mybatis.generator.exception.XMLParserException;

import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;

import java.io.IOException;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

public class MBGTest {

public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {

List warnings = new ArrayList();

boolean overwrite = true;

File configFile = new File("mbg.xml");

ConfigurationParser cp = new ConfigurationParser(warnings);

Configuration config = cp.parseConfiguration(configFile);

DefaultShellCallback callback = new DefaultShellCallback(overwrite);

MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

myBatisGenerator.generate(null);

}

}

逆向工程生成

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序获取手机号,后端JAVA解密流程代码
下一篇:解决IDEA和CMD中java命令提示错误: 找不到或无法加载主类的问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~