完整项目可以参考:https://gitee.com/cary-zhai/ssmdemo.git 完整笔记:https://note.youdao.com/s/7MdA6JTL
一、数据库搭建
create database `ssmbuild`;
create table `books`(
`bookID` int(10) not null auto_increment,
`bookName` varchar(100) not null,
`bookCounts` int(11) not null,
`detail` varchar(200) not null,
key `bookID`(`bookID`)
)engine = innodb default charset=utf8
insert into `books`(`bookID`, `bookName`, `bookCounts`, `detail`) values
(1, 'JavaSE', 100, '入门'),
(2, 'JavaWeb', 100, '进阶'),
(3, 'Spring', 50, '放弃');
二、依赖及问题
文件打包时可能没有静态资源,则需要添加以下配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
<include>**/*.jsp</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
三、代码
搭建框架 - pojo、dao、service、controller
在pojo包下新建实体类(注意:字段名一致、类名与表名一致)
package com.ssmdemo.pojo;
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
public int getBookID() {
return bookID;
}
public void setBookID(int bookID) {
this.bookID = bookID;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookCounts() {
return bookCounts;
}
public void setBookCounts(int bookCounts) {
this.bookCounts = bookCounts;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
在resources下新建applicationContext.xml、spring-dao.xml、spring-service.xml、springmvc-servlet.xml、mybatis-config.xml、jdbc.properties
applicationContext.xml作为容器,导入有关配置文件spring-dao.xml、spring-service.xml、springmvc-servlet.xml
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<import resource="spring-dao.xml"></import>
<import resource="spring-service.xml"></import>
<import resource="springmvc-servlet.xml"></import>
</beans>
spring-dao.xml
<?xml version="1.0" encoding="UTF8"?>
<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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.ssmdemo.dao"></property>
</bean>
</beans>
BookMapper.interface
package com.ssmdemo.dao;
import com.ssmdemo.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
public List<Books> QueryBooks();
public Books QueryBooksById(@Param("bookID") int id);
public int InsertBooks(Books books);
public int DeleteBooksById(@Param("bookID") int id);
public int UpdateBooks(Books books);
}
BookMapper.xml(每次进行新的映射文件创建时,就要及时的再mybatis-config.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.ssmdemo.dao.BookMapper">
<select id="QueryBooks" resultType="books">
select * from `books`;
</select>
<select id="QueryBooksById" resultType="books">
select * from `books` where bookID=#{bookID};
</select>
<insert id="InsertBooks" parameterType="books">
insert into `books`(bookID, bookName, bookCounts, detail) values(#{bookID}, #{bookName}, #{bookCounts}, #{detail});
</insert>
<delete id="DeleteBooksById" parameterType="int">
delete from `books` where bookID=#{bookID};
</delete>
<update id="UpdateBooks" parameterType="books">
update `books` set bookName=#{bookName}, bookCounts=#{bookCounts}, detail=#{detail} where bookID=#{bookID};
</update>
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.ssmdemo.pojo"/>
</typeAliases>
<mappers>
<mapper resource="com/ssmdemo/dao/BookMapper.xml"></mapper>
</mappers>
</configuration>
BooksService.interface
package com.ssmdemo.service;
import com.ssmdemo.pojo.Books;
import java.util.List;
public interface BooksService {
public List<Books> QueryBooks();
public Books QueryBooksById(int id);
public int InsertBooks(Books books);
public int DeleteBooksById(int id);
public int UpdateBooks(Books books);
}
BookServiceImpl.class
package com.ssmdemo.service;
import com.ssmdemo.dao.BookMapper;
import com.ssmdemo.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BooksService {
private BookMapper bookMapper;
public void setBookMapper(BookMapper bookMapper){
this.bookMapper = bookMapper;
}
public List<Books> QueryBooks() {
return bookMapper.QueryBooks();
}
public Books QueryBooksById(int id) {
return bookMapper.QueryBooksById(id);
}
public int InsertBooks(Books books) {
return bookMapper.InsertBooks(books);
}
public int DeleteBooksById(int id) {
return bookMapper.DeleteBooksById(id);
}
public int UpdateBooks(Books books) {
return bookMapper.UpdateBooks(books);
}
}
spring-service.xml(将业务类注入到Spring中时,需要注意spring-dao.xml配置文件中的扫描dao包这一步骤;注入数据源时,也叶要注意到绑定数据源的步骤)
<?xml version="1.0" encoding="UTF8"?>
<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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ssmdemo.service"></context:component-scan>
<bean id="bookServiceImpl" class="com.ssmdemo.service.BookServiceImpl">
<property name="bookMapper" ref="bookMapper"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
springmvc-servlet.xml(配置文件建设完之后根据文件前缀创建文件夹,后缀是文件根据文件类型创建)
<?xml version="1.0" encoding="UTF8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.ssmdemo.controller"></context:component-scan>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Controller.class
package com.ssmdemo.controller;
import com.ssmdemo.pojo.Books;
import com.ssmdemo.service.BookServiceImpl;
import com.ssmdemo.service.BooksService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/bookController")
public class BookController {
@Autowired
@Qualifier("bookServiceImpl")
private BooksService booksService;
public void setBookController(BooksService booksService) {
this.booksService = booksService;
}
@RequestMapping("/queryAllBook")
public String list(Model model){
Books book = booksService.QueryBooksById(1);
model.addAttribute("book", book.getBookName());
return "allBook";
}
}
新建allBook.jsp页面
配置Tomcat 如果没有lib文件夹则需要手动添加并导入包
– 至此SSM框架基本构建结束 –
|