關於IDEA執行ssm專案的一些坑以及ssm的基本的配置
匯入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.timous</groupId>
<artifactId>springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--spring-mvc 其中包含了很多的 spring-aop等一些列和springmvc的一些jar包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--mybatis-spring spring和mybatis的整合需要的jar包-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--lombok的jar包-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!--c3p0的jar包 這裡我們的資料來源使用c3p0的jar-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--jdbc的包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--jdbc connector-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
<!--靜態資源匯出,主要是mapper.xml的匯出-->
<build>
<resources>
<resource>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
建立一個資料庫
create database ssmproject;
create table book(
id int(10) primary key not null auto_increment,
name varchar(20) ,
description varchar(255)
);
# 編寫基本的配置檔案
這裡我們通過基本三層結構來建立檔案,這樣方便記憶
此外還有一個database.properties的配置資料庫的檔案沒有寫,下面會有其他的配置檔案
database.properties配置檔案
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmproject?serverTimezone=UTC&&useSSL=false
jdbc.username=root
jdbc.password=123456 # 你自己的資料庫的密碼
spring-dao配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--配置資料庫的一些配置檔案,主要是讀取資料庫的配置-->
<bean id="propertiesConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<!--配置資料來源 dataSource 這裡使用c3p0的資料庫連線池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置sqlsessionfactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--注入配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--掃描基本的包-->
<property name="basePackage" value="com.timous.dao"/>
</bean>
</beans>
spring-service配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--掃描service的介面,便於註解實現-->
<context:component-scan base-package="com.timous.service"/>
<!--配置資料來源,可以實現事務-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--aop支援等-->
</beans>
spring-mvc配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--註解驅動-->
<mvc:annotation-driven/>
<!--靜態資源管理-->
<mvc:default-servlet-handler/>
<!--掃描註解-->
<context:component-scan base-package="com.timous.controller"/>
<!--試圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/templates/"/>
<property name="suffix" value=".html"/> <!--這裡配置其他的也可以的-->
</bean>
</beans>
mybatis-config配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--別名配置-->
<typeAliases>
<package name="com.timous.entity"/>
</typeAliases>
<!--註冊我們寫過的mapper檔案-->
<mappers>
</mappers>
</configuration>
applicationContext.xml配置檔案
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--把他們整合到一個配置檔案中就可以了,便於我們web.xml的配置-->
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
<import resource="spring-mvc.xml"/>
</beans>
這樣配置檔案就寫好了,下面我們可以在建立webapp整合web,建立資料夾如下
這裡我自己是建立在main下面的,然後自己添進web的。
然後修改你自己的路徑,注意這裡是你自己新增的路徑,一定要新增web.xml
下面的這個路徑指向webapp就行了,結果是webapp下面新增了一個小藍圓圈
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.timous</groupId>
<artifactId>springmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--spring-mvc 其中包含了很多的 spring-aop等一些列和springmvc的一些jar包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!--mybatis-spring spring和mybatis的整合需要的jar包-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<!--lombok的jar包-->
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!--c3p0的jar包 這裡我們的資料來源使用c3p0的jar-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!--jdbc的包-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
</dependencies>
<!--靜態資源匯出,主要是mapper.xml的匯出-->
<build>
<resources>
<resource>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
到這整合的問題基本上市已經完成了,剩下的就是測試的問題了
entity
package com.timous.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Repository;
@Repository
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
int id;
String name;
String description;
}
dao層
//介面
package com.timous.dao;
import com.timous.entity.Book;
import java.util.List;
@Component
public interface BookMapper {
List<Book> queryAllBook();
}
Mapper配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.timous.dao.BookMapper">
<!--編寫sql語句-->
<select id="queryAllBook" resultType="Book">
select * from book
</select>
</mapper>
注意在寫完mapper配置檔案之後要註冊
service層
//介面
package com.timous.service;
import com.timous.entity.Book;
import java.util.List;
public interface BookService {
List<Book> queryAllBook();
}
//實現類
package com.timous.service;
import com.timous.dao.BookMapper;
import com.timous.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BookMapper bookMapper;
public List<Book> queryAllBook() {
return bookMapper.queryAllBook();
}
}
controller層
package com.timous.controller;
import com.timous.entity.Book;
import com.timous.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@ResponseBody
@RequestMapping("/test")
public String test(){
List<Book> books = bookService.queryAllBook();
for (Book book:books) {
System.out.println(book.toString());
}
return "hello world";
}
}
說明專案的型別
配置tomcat
然後執行測試訪問
http://localhost:8080/ssmproject/test
看到下面的頁面就成功了
再去看一下後臺的
也能夠訪問到資料
相關文章
- ssm的配置檔案SSM
- idea搭建簡易ssm專案IdeaSSM
- SSM衍生的配置檔案SSM
- idea ssm maven專案搭建筆記IdeaSSMMaven筆記
- 配置一個簡單的傳統SSM專案SSM
- 【SSM框架整合】專案xml檔案、properties等檔案的配置SSM框架XML
- 關於IDEA中SSM專案Web工程引入pom.xml後仍然找不到包的問題IdeaSSMWebXML
- 【SSM整合】-Maven管理SSM框架的pom.xml配置SSMMaven框架XML
- SSM框架整合(配置檔案)SSM框架
- SSM框架pom配置檔案SSM框架
- SSM框架執行機制SSM框架
- 關於SSM框架的一個簡單DemoSSM框架
- 用idea搭建SSM專案,原來這麼簡單IdeaSSM
- 搭建一個SSM專案SSM
- SSM專案整合——後端SSM後端
- 專案開發框架-SSM框架SSM
- 使用xml檔案配置SSM整合XMLSSM
- SSM框架-Intellij IDEASSM框架IntelliJIdea
- 用IDEA搭建SSM框架IdeaSSM框架
- 【SSM】WEB專案中的中文亂碼問題SSMWeb
- springBoot快速搭建簡單的SSM專案Spring BootSSM
- IDEA配置Maven執行一個簡單的專案IdeaMaven
- SSM 電影后臺管理專案SSM
- 關於SSM與echart結合的問題總結SSM
- SSM專案搭建及實現簡單的登入SSM
- SSM框架的整合SSM框架
- IDEA配置SSM(Spring-Spring MVC-MyBatisIdeaSSMSpringMVCMyBatis
- SSM完整專案(內含原始碼)SSM原始碼
- Day73 SSM專案 搭建框架SSM框架
- SSM(十) 專案重構-網際網路專案的Maven結構SSMMaven
- 基於SSM的職員考勤系統SSM
- ssm專案的搭建:使用到框架 spring springmvc mybatisSSM框架SpringMVCMyBatis
- SSM視訊教程:Java進階SSM分散式專案實戰視訊教程SSMJava分散式
- centos下配置nginx遇到的一些基本的坑CentOSNginx
- 搭建ssm的領悟SSM
- SSM三大框架的執行流程、原理、核心技術詳解SSM框架
- 模仿天貓實戰【SSM版】——專案起步SSM
- 01 整合IDEA+Maven+SSM框架的高併發的商品秒殺專案之業務分析與DAO層IdeaMavenSSM框架