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

本文详细介绍如何使用Spring Boot快速搭建SSM框架,并实现基于Oracle数据库的简单增删查改操作。主要内容包括:IDEA环境下Spring Boot项目的创建过程,MyBatis、Thymeleaf等依赖的配置方法,以及具体代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot创建SSM(IDEA+ORACLE)

1.idea新建项目

新建Spring BOOT项目

新建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 

pom中引用JAR的代码

<dependency>  
    <groupId>com.oracle</groupId>  
    <artifactId>ojdbc6</artifactId>  
    <version>11.1.0.7.0</version>  
</dependency> 

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

因为用了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 结束

因为使用mybatis,同样要添加相应配置

##Mybatis扫描的位置
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#配置文件地址
mybatis.config-location=classpath:mybatis-config.xml

因为我这里使用了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>

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.编写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不再详述,请自己添加
}

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);
}

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);
}

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);
    }
}

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";
    }
}

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>

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
这里写图片描述

出现你插入的信息表示你成功了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值