1.1maven軟件官網下載便可 連接http://maven.apache.org/ 點擊下載css
解壓到根目錄下完成java
連接:https://pan.baidu.com/s/1E6cqODfAeZgd2UtSF2FQww
提取碼:z7am
複製這段內容後打開百度網盤手機App,操做更方便哦--來自百度網盤超級會員V1的分享 文件解壓後即爲本地倉庫mysql
打開D:\apache-maven-3.6.3-bin\apache-maven-3.6.3\conf下的settings.xml文件web
編輯 <localRepository>/path/to/local/repo</localRepository>spring
例如個人本地倉庫在d盤下的D:\apache-maven-3.6.3-bin\repository_bootsql
則配置以下<localRepository>D:\apache-maven-3.6.3-bin\repository_boot</localRepository>數據庫
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->express
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>apache
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>api
<mirror>
<id>repo2</id>
<name>Mirror from Maven Repo2</name>
<url>http://repo2.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
</mirrors>
將maven集成到idea中完畢
修改D:\apache-tomcat-9.0.41\conf\logging.properties文件中的全部的UTF8改成GBK
將idea file encodeing改成utf-8發佈項目時不會出現中文亂碼的狀況
create database ssm; use ssm; create table account( id int primary key auto_increment, name varchar(100), money double(7,2) );
爲避免建立項目時過慢在Properties中添加 archetypeCatalog internal 以下圖
建立完項目後添加應有的類
結構以下
1 package com.ssm.controller; 2 import com.ssm.service.AccountService; 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 @Controller 7 @RequestMapping("/account") 8 public class AccountController { 9 @Autowired 10 private AccountService accountService; 11 @RequestMapping("/findAll") 12 public String findAll(){ 13 System.out.println("表現層 全部信息"); 14 accountService.findAll(); 15 return "list"; 16 } 17 } 18 19 20 package com.ssm.dao; 21 22 import com.ssm.domaiin.Account; 23 import org.apache.ibatis.annotations.Insert; 24 import org.apache.ibatis.annotations.Select; 25 import org.springframework.stereotype.Repository; 26 27 import java.util.List; 28 29 @Repository 30 public interface AccountDao { 31 @Select("select * from account") 32 List<Account> findAll(); 33 @Insert("insert into account(name,money) values(#{name},#{money})") 34 void saveAccount(Account account); 35 } 36 37 38 package com.ssm.domaiin; 39 40 import java.io.Serializable; 41 42 public class Account implements Serializable { 43 private Integer id; 44 private String name; 45 private Float money; 46 47 @Override 48 public String toString() { 49 return "Account{" + 50 "id=" + id + 51 ", name='" + name + '\'' + 52 ", money=" + money + 53 '}'; 54 } 55 56 public Integer getId() { 57 return id; 58 } 59 60 public void setId(Integer id) { 61 this.id = id; 62 } 63 64 public String getName() { 65 return name; 66 } 67 68 public void setName(String name) { 69 this.name = name; 70 } 71 72 public Float getMoney() { 73 return money; 74 } 75 76 public void setMoney(Float money) { 77 this.money = money; 78 } 79 } 80 81 82 package com.ssm.service.impl; 83 84 import com.ssm.dao.AccountDao; 85 import com.ssm.domaiin.Account; 86 import com.ssm.service.AccountService; 87 import org.springframework.beans.factory.annotation.Autowired; 88 import org.springframework.stereotype.Service; 89 90 import java.util.List; 91 @Service("accountService") 92 public class AccountServiceImpl implements AccountService { 93 @Autowired 94 private AccountDao accountDao; 95 96 public List<Account> findAll() { 97 System.out.println("業務層查詢全部用戶"); 98 99 System.out.println(accountDao.findAll()); 100 return null; 101 } 102 103 public void saveAccount(Account account) { 104 System.out.println("業務層 保存用戶"); 105 } 106 } 107 108 package com.ssm.service; 109 110 import com.ssm.domaiin.Account; 111 112 import java.util.List; 113 114 public interface AccountService { 115 116 List<Account> findAll(); 117 118 void saveAccount(Account account); 119 }
用於測試spring mybatis的代碼以下
package com.ssm.test; import com.ssm.dao.AccountDao; import com.ssm.domaiin.Account; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class TestMybatis { @Test public void testFindAll(){ try { InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session= factory.openSession(); AccountDao aDao = session.getMapper(AccountDao.class); List<Account> list = aDao.findAll(); System.out.println(list); session.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } package com.ssm.test; import com.ssm.service.AccountService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring { @Test public void testFindAll(){ ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = ac.getBean("accountService", AccountService.class); accountService.findAll(); } }
在resource文件夾下創建applicationContext.xml springmvc.xml SqlMapConfig.xml jdbcConfig.properties
springmvc.xml內容
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <context:component-scan base-package="com.ssm"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 視圖解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 過濾靜態資源--> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/images/**" location="/images/"/> <!-- 開啓註解支持--> <mvc:annotation-driven/> </beans>
jdbcConfig.properties內容 mysql用的是8.0版本
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
若是是.xml格式的把&符號改成&
applicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.ssm"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 整合mybatis--> <context:property-placeholder location="classpath:jdbcConfig.properties" /> <!-- 鏈接池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"/> </bean> <!-- accountdao接口所在的包--> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.dao"/> </bean> </beans>
最後一步 在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
項目中所需依賴以下
<properties> <spring.version>5.0.2.RELEASE</spring.version> <slf4j.version>1.6.6</slf4j.version> <log4j.version>1.2.12</log4j.version> <shiro.version>1.2.3</shiro.version> <mysql.version>8.0.11</mysql.version> <mybatis.version>3.4.5</mybatis.version> </properties> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> </dependencies>
根據需求改變mysql驅動版本