1.Spring Boot创建SSM(IDEA+ORACLE)

Spring Boot创建SSM(IDEA+ORACLE)
1.idea新建项目
新建Spring BOOT项目


定义项目


新加依赖,oracle没法加自己手动加
web基础依赖


mybatis


thymeleaf


下一步点击完成即可

刚刚没有加oracle,JDBC的包现在要自己加了
先在maven库里加上jar包,在maven中执行,红色是你的驱动的路径

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.1.0.7.0 -Dpackaging=jar -Dfile=D:\App\instantclient_11_2\ojdbc6.jar
1
pom中引用JAR的代码

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
1
2
3
4
5
2.编写项目的配置aplication.properties
配置号项目的dataSource
spring.datasource.url=jdbc:oracle:thin:@IP:port:SID
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.jpa.database = oracle
1
2
3
4
5
因为用了thymeleaf所以也要添加配置
想看注入的详细信息可以查看
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties

#thymeleaf配置开始

#thymeleaf视图解析器此处是默认配置
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html

spring.thymeleaf.mode=HTML5

#取消thymeleaf对页面的强制校验
#spring.thymeleaf.mode=LEGACYHTML5

spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false

#thymeleaf 结束
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
因为使用mybatis,同样要添加相应配置
##Mybatis扫描的位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#配置文件地址
mybatis.config-location=classpath:mybatis-config.xml
1
2
3
4
因为我这里使用了Mybatis的别名所以使用了配置文件mybatis-config.xml,这里的KeyHashMap是我写的一个工具类,下面有所涉及

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<typeAlias type="java.lang.Integer" alias="Integer"/>
<typeAlias type="java.lang.String" alias="String"/>
<typeAlias type="java.lang.Object" alias="obj"/>
<typeAlias type="com.jyq.utils.KeyHashMap" alias="khm"/>
<typeAlias type="java.util.Map" alias="m"/>
</typeAliases>
</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
3.开始编写项目代码结构如图


在这个代码中使用 两个工具类 BeanUtils 和 KeyHashMap 其中 BeanUtils没有使用,可以忽略.
KeyHashMap 是一个拓展的 HashMap ,他可以把你从数据库查出来的字段由 A_B转换成 aB 的驼峰法则,使用这些的目的是尽量不使用JavaBean,来提高程序的可可拓展性.
有兴趣的可以使用,没有兴趣的可以使用JavaBean来代替.
源码:

package com.jyq.utils;


import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class KeyHashMap<K, V> extends HashMap<K, V>
{
@Override
public V put(K key, V value)
{
return super.put(this.toLower(key), value);
}

private K toLower(Object tem)
{
return (K)underlineToCamel2(tem.toString());
}

public static final char UNDERLINE='_';
public static String camelToUnderline(String param){
if (param==null||"".equals(param.trim())){
return "";
}
int len=param.length();
StringBuilder sb=new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c=param.charAt(i);
if (Character.isUpperCase(c)){
sb.append(UNDERLINE);
sb.append(Character.toLowerCase(c));
}else{
sb.append(c);
}
}
return sb.toString();
}
public static String underlineToCamel(String param){
if (param==null||"".equals(param.trim())){
return "";
}
int len=param.length();
StringBuilder sb=new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c=param.charAt(i);
if (c==UNDERLINE){
if (++i<len){
sb.append(Character.toUpperCase(param.charAt(i)));
}
}else{
sb.append(c);
}
}
return sb.toString();
}
public static String underlineToCamel2(String param){
if (param==null||"".equals(param.trim())){
return "";
}
param=param.toLowerCase();
StringBuilder sb=new StringBuilder(param);
Matcher mc= Pattern.compile("_").matcher(param);
int i=0;
while (mc.find()){
int position=mc.end()-(i++);
//String.valueOf(Character.toUpperCase(sb.charAt(position)));
sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
}
return sb.toString();
}

}

1
2
3
4
5
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
1.新建表表结构如下,自己插入一条数据


2.编写model
package com.hand.demo.model;

/**
* Created by JYQ on 2017/11/13.
*/
public class ResouceTable
{
private Integer sourceId;
private String sourceName;
private String sourceType;
private String sourceSize;
private String sourcePwd;
private String sourceDesc;
//get/set不再详述,请自己添加
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
3.开始编写DAO层
package com.hand.demo.dao;

import com.hand.demo.model.ResouceTable;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

/**
* Created by JYQ on 2017/11/13.
*/
@Repository
@Mapper
public interface ResourceTableDao {

public List<Map<String,String>> query( ResouceTable resouceTable);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4.开始编写service层


package com.hand.demo.service;
import com.hand.demo.model.ResouceTable;
import java.util.List;
import java.util.Map;
/**
* Created by JYQ on 2017/11/13.
*/
public interface ResourceTableService {
public List<Map<String,String>> query( ResouceTable resouceTable);
}

1
2
3
4
5
6
7
8
9
10
11
12
package com.hand.demo.service.impl;

import com.hand.demo.dao.ResourceTableDao;
import com.hand.demo.model.ResouceTable;
import com.hand.demo.service.ResourceTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;

/**
* Created by JYQ on 2017/11/13.
*/
@Service
@Transactional
public class ResourceTableServiceImpl implements ResourceTableService {

@Autowired
private ResourceTableDao resourceTableDao;

public List<Map<String,String>> query( ResouceTable resouceTable) {
return resourceTableDao.query(resouceTable);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
5.controller层
package com.hand.demo.controller;

import com.hand.demo.model.ResouceTable;
import com.hand.demo.service.ResourceTableService;
import com.jyq.utils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
* Created by JYQ on 2017/11/13.
*/
@Controller
@RequestMapping("res")
public class ResourceTableController {

@Autowired
private ResourceTableService resourceTableService;

@RequestMapping("query")
public String query(Model model, ResouceTable resouceTable)
{
model.addAttribute("res",resourceTableService.query(resouceTable));
return "resource_table";
}
}
1
2
3
4
5
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
6.Mybatis的Mapper文件
ResouceTableMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hand.demo.dao.ResourceTableDao" >

<!--khm 是配置文件的别名,本体是KeyHashMap-->
<select id="query" resultType="khm">
SELECT * FROM RESOURCE_TABLE
</select>
</mapper>
1
2
3
4
5
6
7
8
9
7.HTML页面
resource_table.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Product list</h1>
<table>
<tr>
<th>资源名称</th>
<th>资源类型</th>
<th>资源大小</th>
</tr>
<tr th:each="re:${res}">
<td th:text="${re.sourceName}"></td>
<td th:text="${re.sourceType}"></td>
<td th:text="${re.sourceSize}"></td>
</tr>
</table>
</body>
</html>
 
4.启动项目,访问
点击idea的debugger启动(start也行,我习惯于debugger,用于打断点)


完成后访问http://localhost:8080/res/query


出现你插入的信息表示你成功了
————————————————
版权声明:本文为CSDN博主「水淹萌龙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_21047625/article/details/78537819

posted @ 2022-09-23 09:56  superstar  阅读(51)  评论(0编辑  收藏  举报