前言
本文所提及文件 conf.zip
1.打开idea
2.create new project
等待载入相应web jar包
3.初始化完了以后再main文件夹下面创建java和resources,直接选择下面两个选项就行了
如果创建的文件夹是灰色的,执行下图操作
java文件夹选择source root ,resources选择resources root
然后再java文件夹下面创建以下包
其中service还有一个子包,叫impl或者core,service放接口,impl(core)放继承类
4.将以下三个文件拷贝到resources路径下并创建一个mapper文件夹(同一级别)
5.打开pom文件,此文件用于下载maven依赖
用下面表格里面的代码替换原pom的对应标签对,如截图
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>4.3.4.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.4.1</mybatis.version> </properties> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- 实现slf4j接口并整合 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.3</version> </dependency> <!-- 数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> <scope>runtime</scope> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- MyBatis --> <!-- <dependency>--> <!-- <groupId>org.mybatis</groupId>--> <!-- <artifactId>mybatis</artifactId>--> <!-- <version>${mybatis.version}</version>--> <!-- </dependency>--> <!-- <!– mybatis/spring整合包 –>--> <!-- <dependency>--> <!-- <groupId>org.mybatis</groupId>--> <!-- <artifactId>mybatis-spring</artifactId>--> <!-- <version>1.3.1</version>--> <!-- </dependency>--> <!--mybatisplus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.4.2</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- 实现 jsp 页面的复用 --> <dependency> <groupId>com.googlecode.rapid-framework</groupId> <artifactId>rapid-core</artifactId> <version>4.0.5</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- jstl-api --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <!-- jstl-impl --> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jstl-impl</artifactId> <version>1.2</version> </dependency> <!-- standard --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- jsf-api --> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!-- jsf-api --> <!-- https://mvnrepository.com/artifact/javax.faces/jsf-impl --> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-impl</artifactId> <version>1.2_12</version> </dependency> <!-- commons系列包 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <!-- haspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> <scope>runtime</scope> </dependency> <!--分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl --> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency> <!--fastjson包--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-spec</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <!-- 定时器jar包 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!--网络请求--> <!--lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> </dependencies>
替换之后会出现下图所示的图标,点击更新依赖
6.更新依赖后在idea启用以下功能
在此之前先下载lombok插件,小辣椒一样的图标(setting --> Plugins)
7.然后打开web.xml文件用以下代码全部替换
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 字符集过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>fileEncoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- <error-page>--> <!-- <error-code>404</error-code>--> <!-- <location>/page/error-page/404.jsp</location>--> <!-- </error-page>--> <!-- <!– <error-page>–>--> <!-- <!– <error-code>500</error-code>–>--> <!-- <!– <location>/page/error-page/500.jsp</location>–>--> <!-- <!– </error-page>–>--> <!-- <error-page>--> <!-- <error-code>405</error-code>--> <!-- <location>/page/error-page/405.jsp</location>--> <!-- </error-page>--> </web-app>
web.xml其实就是javaweb的启动类,首先加载此文件,一般配置监听器拦截器等
后期详细讲解
8打开applicationContext.xml文件,这个文件是spring的配置文件
8.1
改成你项目的包名,这个是配置自动装配(注解)的扫描
8.2
这个修改为持久层所在的包,也就是包名+.mapper
8.3
这个是aop(面向切面编程)配置,修改为包名
9.打开springmvc.xml文件,此文件是springmvc的配置文件
后面讲解具体配置作用
9.1
这个是配置控制层注解扫描,修改为包名+.controller
9.2
这一行很重要,没有这个配置会访问不到静态资源如css,js,html
10.最后来看database.properties这个文件,配置了数据库访问地址,账号密码,连接池等配置
一般只需要该账号密码和数据库
10.1
数据库驱动,这个是mysql最新驱动,8.0以上必须用这个,以下也可以用这个
10.2
数据库地址,mysql8.0以后需要指定时区
10.3
数据库账号密码
11.开始第一个controller层测试
package org.example.controller; import com.alibaba.fastjson.JSONObject; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(value = "test/{id}") public JSONObject test (@PathVariable("id")String id,String name){ //id是通过地址获取,name是地址后面?跟的参数或者post的表单数据 JSONObject jsonObject= new JSONObject(); jsonObject.put("name",name); jsonObject.put("id",id); return jsonObject; } }
示例项目为 Maven
mybatis_plus文档
https://mp.baomidou.com/guide/
未完待续。。。。。
Comments | 59 条评论
博客作者 pharmacy drugstore online
Does your blog have a contact page? I’m having trouble locating it but, I’d like to shoot you an email. I’ve got some ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.
博客作者 canadian pharmaceuticals online
Whoa! This blog looks just like my old one! It’s on a totally different topic but it has pretty much the same layout and design. Great choice of colors!
博客作者 canadian pharmaceuticals
You can certainly see your skills in the work you write. The arena hopes for more passionate writers such as you who aren’t afraid to mention how they believe. All the time go after your heart.
博客作者 drugstore online shopping
Today, I went to the beach front with my children. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!
博客作者 canadian pharmaceuticals online
We’re a group of volunteers and starting a new scheme in our community. Your web site offered us with valuable info to work on. You’ve done an impressive job and our entire community will be thankful to you.
博客作者 canada pharmaceuticals online generic
you are in reality a excellent webmaster. The website loading pace is incredible. It sort of feels that you are doing any distinctive trick. In addition, The contents are masterpiece. you have performed a wonderful process in this subject!
博客作者 canadian pharmaceuticals online safe
I like the helpful information you provide to your articles. I will bookmark your weblog and test again here regularly. I’m moderately certain I’ll be told many new stuff proper right here! Best of luck for the next!
博客作者 canadian pharmaceutical companies
Hello, its pleasant paragraph concerning media print, we all be familiar with media is a impressive source of data.
博客作者 canadian pharmaceuticals for usa sales
Hi mates, how is the whole thing, and what you desire to say concerning this paragraph, in my view its actually amazing in favor of me.
博客作者 canadian pharcharmy online
Great blog here! Also your site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as fast as yours lol
博客作者 pharmaceuticals online australia
Inspiring story there. What occurred after? Take care!
博客作者 canadian pharmacy generic viagra
you are truly a good webmaster. The web site loading velocity is amazing. It sort of feels that you’re doing any unique trick. In addition, The contents are masterpiece. you’ve done a great activity in this matter!
博客作者 list of canadian pharmaceuticals online
Remarkable! Its truly remarkable piece of writing, I have got much clear idea on the topic of from this paragraph.
博客作者 buy viagra pharmacy 100mg
Hello there! This is my 1st comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading through your articles. Can you suggest any other blogs/websites/forums that deal with the same subjects? Many thanks!
博客作者 canadian pharmaceuticals online
Hola! I’ve been following your website for a long time now and finally got the courage to go ahead and give you a shout out from Huffman Tx! Just wanted to mention keep up the fantastic job!