java学习知识
SSM框架–Spring+SpringMVC+Mybatis (IDEA)搭建
使用idea创建一个maven项目( 这里演示 的是 web项目)
点击 Finish
然后开始配置 pom.xml文件(添加各种依赖jar包)
先去找 spring 所需的 jar包
jar包中心仓库地址:http://mvnrepository.com/
找到 Spring Web ( 因为 我这里演示的是一个web项目)
把这段 复制到 pom.xml文件中
1 <?xml version="1.0" encoding="UTF-8"?>
2
3
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 4.0.0
6
7 com.oukele.demo
8 SSM_demo
9 1.0-SNAPSHOT
10 war
11
12 SSM_demo Maven Webapp
13
14 http://www.example.com
15
16
17 UTF-8
18 1.7
19 1.7
20
21
22
23
24
25
26
27
28 org.springframework
29 spring-web
30 5.1.0.RELEASE
31
32
33
34 org.springframework
35 spring-aop
36 5.1.0.RELEASE
37
38
39
40 org.springframework
41 spring-jdbc
42 5.1.0.RELEASE
43
44
45
46 org.springframework
47 spring-webmvc
48 5.1.0.RELEASE
49
50
51
52
53 org.mybatis
54 mybatis
55 3.4.6
56
57
58
59 org.mybatis
60 mybatis-spring
61 1.3.2
62
63
64
65
66 org.mariadb.jdbc
67 mariadb-java-client
68 2.3.0
69
70
71
72
73 com.mchange
74 c3p0
75 0.9.5.2
76
77
78
79
80 javax.servlet
81 jstl
82 1.2
83
84
85
86
87 junit
88 junit
89 4.11
90 test
91
92
93
94
95
96
97 SSM_demo
98
99
100
101 maven-clean-plugin
102 3.0.0
103
104
105
106 maven-resources-plugin
107 3.0.2
108
109
110 maven-compiler-plugin
111 3.7.0
112
113
114 maven-surefire-plugin
115 2.20.1
116
117
118 maven-war-plugin
119 3.2.0
120
121
122 maven-install-plugin
123 2.5.2
124
125
126 maven-deploy-plugin
127 2.8.2
128
129
130
131
132
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
下面是我配置完整的pom.xml文件 ( 这里 我只在dependencies的下面 添加依赖 其他的 我都没有动 )
1 <?xml version="1.0" encoding="UTF-8"?>
2
3
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 4.0.0
6
7 com.oukele.demo
8 SSM_demo
9 1.0-SNAPSHOT
10 war
11
12 SSM_demo Maven Webapp
13
14 http://www.example.com
15
16
17 UTF-8
18 1.7
19 1.7
20
21
22
23
24
25
26
27
28 org.springframework
29 spring-web
30 5.1.0.RELEASE
31
32
33
34 org.springframework
35 spring-aop
36 5.1.0.RELEASE
37
38
39
40 org.springframework
41 spring-jdbc
42 5.1.0.RELEASE
43
44
45
46 org.springframework
47 spring-webmvc
48 5.1.0.RELEASE
49
50
51
52
53 org.mybatis
54 mybatis
55 3.4.6
56
57
58
59 org.mybatis
60 mybatis-spring
61 1.3.2
62
63
64
65
66 org.mariadb.jdbc
67 mariadb-java-client
68 2.3.0
69
70
71
72
73 com.mchange
74 c3p0
75 0.9.5.2
76
77
78
79
80 javax.servlet
81 jstl
82 1.2
83
84
85
86
87 junit
88 junit
89 4.11
90 test
91
92
93
94
95
96
97 SSM_demo
98
99
100
101 maven-clean-plugin
102 3.0.0
103
104
105
106 maven-resources-plugin
107 3.0.2
108
109
110 maven-compiler-plugin
111 3.7.0
112
113
114 maven-surefire-plugin
115 2.20.1
116
117
118 maven-war-plugin
119 3.2.0
120
121
122 maven-install-plugin
2.5.2
124
125
126 maven-deploy-plugin
127 2.8.2
128
129
130
131
132
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
配置完成后
等待 jar包 下载完成。
这些都是下载的jar包(这样pom.xml 文件 算是配置完成了。)
接下来开始 其他 xml文件的配置
main下面 如果 少一个 java 文件 (我们手动新建)
java 文件变成蓝色的就可以了。
建立项目结构
开始配置 spring xml 和 spring mvc xml
在 main 目录里 创建 一个 resources 文件,然后将它标志成 资源文件夹
在resources文件夹中 新建一个 spring_root.xml 文件
spring_root文件的完整配置信息
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:contxt="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1234567891011121314151617181920212223242526272829303132333435
spring_web文件的完整配置信息
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:contxt="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1234567891011121314151617181920212223
在resources 文件夹 新建一个包 ( 包名随意趣吧,有意义一些的)
然后在那个包里新建一个 xml文件 用来映射 DML语法
BookMapper.xml 配置文件信息
1 <?xml version="1.0" encoding="UTF-8" ?>
2 /p>
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5
6
7
8
9
10
11 select * from ssm_book
12
13
14
123456789101112131415
这里xml文件差不多配置完成了。
开始写代码啦。
(entity)实体类。
```xml
1 package com.nf147.bookstore_ssm.entity;
2
3 public class Book {
4 private int bookid;
5 private String name;
6 private int cnt;
7
8 public Book() {
9 }
10
11 public Book(String name, int cnt) {
12 this.name = name;
13 this.cnt = cnt;
14 }
15
16 public int getBookid() {
17 return bookid;
18 }
19
20 public void setBookid(int bookid) {
21 this.bookid = bookid;
22 }
23
24 public String getName() {
25 return name;
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32 public int getCnt() {
33 return cnt;
34 }
35
36 public void setCnt(int cnt) {
37 this.cnt = cnt;
38 }
39
40 @Override
41 public String toString() {
42 return "Book{" +
43 "bookid=" + bookid +
44 ", name='" + name + ''' +
45 ", cnt=" + cnt +
46 '}';
47 }
48 }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
(dao)数据访问层
1 package com.oukele.bookshop_ssm.dao;
2
3 import com.oukele.bookshop_ssm.entity.Book;
4 import org.springframework.stereotype.Repository;
5 import java.util.List;
6
7 @Repository
8 public interface IBookDao {
9 List listAll();
10 }
(service)服务层
1 package com.oukele.bookshop_ssm.service;
2
3 import com.oukele.bookshop_ssm.entity.Book;
4
5 import java.util.List;
6
7 public interface BookService {
8 List listAll();
9 }
1 package com.oukele.bookshop_ssm.service;
2
3 import com.oukele.bookshop_ssm.dao.IBookDao;
4 import com.oukele.bookshop_ssm.entity.Book;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Service;
7
8 import java.util.List;
9
10 @Service
11 public class BookServiceTemp implements BookService {
12
13 // 使用 dao 中的接口
14 @Autowired
15 private IBookDao bookDao;
16
17 @Override
18 public List listAll() {
19 return bookDao.listAll();
20 }
1 package com.oukele.bookshop_ssm.web;
2
3 import com.oukele.bookshop_ssm.entity.Book;
4 import com.oukele.bookshop_ssm.service.BookServiceTemp;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.servlet.ModelAndView;
9
10 import java.util.List;
11
12 @Controller
13 public class BookController {
14
15 @Autowired
16 private BookServiceTemp bookServiceTemp;
17
18 // 请求url
19 @RequestMapping(value = “/books”)
20 public ModelAndView getList(){
21 // 页面的名字
22 ModelAndView mv = new ModelAndView(“list”);
23 // 获取数据
24 List list = bookServiceTemp.listAll();
25 mv.addObject(“book”,list);
26 return mv;
27 }
28
29 }
(web)控制层
1 package com.oukele.bookshop_ssm.web;
2
3 import com.oukele.bookshop_ssm.entity.Book;
4 import com.oukele.bookshop_ssm.service.BookService;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.servlet.ModelAndView;
9
10 import java.util.List;
11
12 @Controller
13 public class BookController {
14
15 @Autowired
16 private BookService bookService;
17
18 // 请求url
19 @RequestMapping(value = “/books”)
20 public ModelAndView getList(){
21 // 页面的名字
22 ModelAndView mv = new ModelAndView(“list”);
23 // 获取数据
24 List list = bookService.listAll();
25 mv.addObject(“book”,list);
26 return mv;
27 }
28
29 }
jsp页面
1
2
3
4
5
Title6
7
8 书籍列表:
9
10
11
12
13
编号14
书名15
数量16
17
18
19
20
${books.bookid}21
${books.name}22
${books.cnt}23
24
25
26
27
28
29
1234567891011121314151617181920212223242526272829
项目结构:
配置 web.xml文件
完整的web.xml文件信息
1 /p>
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4
5
6 Archetype Created Web Application
7
8
9
10 contextConfigLocation
11 classpath:spring_root.xml
12
13
14 org.springframework.web.context.ContextLoaderListener
15
16
17
18
19
20 webs
21 org.springframework.web.servlet.DispatcherServlet
22
23 contextConfigLocation
24 classpath:spring_web.xml
25
26
27
28
29 webs
30 /
31
32
33
123456789101112131415161718192021222324252627282930313233
配置tomcat服务器,并启动。
-------------------------------此文结束---------------------------------------
有什么想法欢迎大家在评论区多多留言![作揖]
欢迎大家对知识点进行补充或者修改![作揖]
喜欢这篇文章的话就点点关注、收藏、转发![爱慕][爱慕]