ssm框架搭建
一:環境準備
jdk:java version "1.8.0_251"
Tomcat:tomacat8
Maven:Apache Maven 3.6.3
二,新建maven工程
工程建立完成,
2,搭建專案結構
專案結構如下
3,Customer.java
package com.liam.pojo; public class Customer { /** * 客戶持久層 */ private Integer id; private String userName; private String jobs;private String phone; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }public String getJobs() { return jobs; } public void setJobs(String jobs) { this.jobs = jobs; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
4,CustomerDao
package com.liam.dao;import com.liam.pojo.Customer; public interface CustomerDao { /** * 根據id 查詢客戶資訊 * @param id * @return */ public Customer findCustomerById(Integer id); }
5,CustomerService
package com.liam.service; import com.liam.pojo.Customer; public interface CustomerService { public Customer findCustomerById(Integer id); }
6,CustomerServiceImpl
package com.liam.service.impl; import com.liam.dao.CustomerDao; import com.liam.pojo.Customer; import com.liam.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDao customerDao; @Override public Customer findCustomerById(Integer id) { return this.customerDao.findCustomerById(id); } }
7,CustomerController
package com.liam.controller; import com.liam.pojo.Customer; import com.liam.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class CustomerController { @Autowired private CustomerService customerService; @RequestMapping("/findCustomerById") public String findCustomerById(Integer id, Model model){ Customer customer = customerService.findCustomerById(id); model.addAttribute("customer",customer); return "customer"; } }
9,
------------恢復內容開始------------
一:環境準備
jdk:java version "1.8.0_251"
Tomcat:tomacat8
Maven:Apache Maven 3.6.3
二,新建maven工程
工程建立完成,
2,搭建專案結構
專案結構如下
3,Customer.java
package com.liam.pojo; public class Customer { /** * 客戶持久層 */ private Integer id; private String userName; private String jobs; private String phone; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getJobs() { return jobs; } public void setJobs(String jobs) { this.jobs = jobs; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
4,CustomerDao
package com.liam.dao; import com.liam.pojo.Customer; public interface CustomerDao { /** * 根據id 查詢客戶資訊 * @param id * @return */ public Customer findCustomerById(Integer id); }
5,CustomerService
package com.liam.service; import com.liam.pojo.Customer; public interface CustomerService { public Customer findCustomerById(Integer id); }
6,CustomerServiceImpl
package com.liam.service.impl; import com.liam.dao.CustomerDao; import com.liam.pojo.Customer; import com.liam.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDao customerDao; @Override public Customer findCustomerById(Integer id) { return this.customerDao.findCustomerById(id); } }
7,CustomerController
package com.liam.controller; import com.liam.pojo.Customer; import com.liam.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class CustomerController { @Autowired private CustomerService customerService; @RequestMapping("/findCustomerById") public String findCustomerById(Integer id, Model model){ Customer customer = customerService.findCustomerById(id); model.addAttribute("customer",customer); return "customer"; } }
9,CustomerDao.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.liam.dao.CustomerDao"> <!--根據id 查詢客戶資訊--> <resultMap id="BaseResultMap" type="com.liam.pojo.Customer"> <!--<result column="id" property="id" jdbcType="id" />--> <result column="userName" property="userName" jdbcType="VARCHAR" /> <result column="jobs" property="jobs" jdbcType="VARCHAR" /> <result column="phone" property="phone" jdbcType="VARCHAR" /> </resultMap> <select id="findCustomerById" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select * from customer where id = #{id} </select> </mapper>
10,mybatis-config.xml
<?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.liam.pojo"/> </typeAliases> </configuration>
11,spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使用註解驅動--> <mvc:annotation-driven/> <!-- 配置檢視解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 掃描所有handler(控制器)--> <context:component-scan base-package="com.liam.controller"/> </beans>
12,spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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"> <context:component-scan base-package="com.liam.service"/> <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!--資料庫連線池--> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${password}"/> <!-- 初始化連線大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連線池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連線池最大空閒 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連線池最小空閒 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連線最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!--配置sqlsession工廠--> <!-- spring和MyBatis完美整合,不需要mybatis的配置對映檔案 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自動掃描mapping.xml檔案 --> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!--配置DAO所在spring會自動查詢下面的類--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.liam.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
13,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"> <display-name>Archetype Created Web Application</display-name> <!--啟動spring容器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--請求和應答字元編碼過濾器--> <filter> <filter-name>encoding-filter</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> </filter> <filter-mapping> <filter-name>encoding-filter</filter-name> <!--<url-pattern>/*</url-pattern>--> <servlet-name>springDispatcherServlet</servlet-name> </filter-mapping> <!-- 用前端控制器初始化springmvc容器 --> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
14,pom.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.example</groupId> <artifactId>ssmTest2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ssmTest2 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.18.RELEASE</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <!-- 資料庫事務管理--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> </dependencies> <build> <finalName>ssmTest2</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
15,customer.jsp
<%-- Created by IntelliJ IDEA. User: 31436 Date: 2020/9/3 Time: 22:41 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>客戶資訊</title> </head> <body> <table border=1> <tr> <td>編號</td> <td>名稱</td> <td>職業</td> <td>電話</td> </tr> <tr> <td>${customer.id}</td> <td>${customer.userName}</td> <td>${customer.jobs}</td> <td>${customer.phone}</td> </tr> </table> </body> </html>
16,sql
CREATE TABLE customer( id int(32) PRIMARY key AUTO_INCREMENT, username varchar(50), jobs varchar(50), phone varchar(16) );
INSERT INTO customer VALUES (1, 'liam', 'worker', 123456789);
INSERT INTO customer VALUES (2, 'wlin', 'teacher', 987654321);
17,執行結果
總結:
1,網上的參考資料比較多,主要參考了:
https://www.cnblogs.com/wanson/articles/9998829.html
2,在我第一次搭建的時候,使用的是tomacat10,結果一直出現
解決無果,換用tomacat8,未出現上面的問題。
2,
java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Intege
mybatis配置的jdbcType型別要是大寫的,檢查了mybatis的配置檔案,果然是這個問題
3,
Could not get JDBC Connection; nested exception isorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
參考此連結:
https://blog.csdn.net/weixin_41955327/article/details/82286309
報錯之前為 userName -> user,對應的
4,
There is no setter for property named 'dd_valueId' in 'class com.manager.pojo.DataDictionary'的錯誤解決方法
mybatis 對映檔案和 Bean應該對應一致。
5,
使用Idea部署SSM專案後,訪問路徑為url:8080/專案名_war_exploded的解決方案
在tomcat配置頁的Deployment下,修改Application context為/,即可直接使用url:8080訪問專案主頁。或者任意你想要的路徑,
6,
Result Maps collection does not contain value for 錯誤
當時未新增resultMap 資料型別,parameterType 型別也選的不正確。按照上面正確的配置即可
7,
java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
錯誤資訊翻譯—— java.sql.SQLException: 伺服器時區值'?? u±e×??±??未被識別或代表多個時區。如果希望利用時區支援,必須配置伺服器或JDBC驅動程式(通過serverTimezone配置屬性),以使用更特殊的時區值。
解決方法:JDBC連線的URL後面加上serverTimezone = GMT,如果需要使用GMT + 8時區,需要寫成GMT%2B8。
追加上後面這些
8,使用Idea構建springmvc框架,出現no bean named ‘cacheManager’ is defined 錯誤。
參考連結
https://blog.csdn.net/qq_23184291/article/details/78086398
9,整個工程參考連結