完整SSM框架图书管理系统开发流程
环境:
IDEA
MySQL 5.5.62
Tomcat 8.5.61
Maven 3.6
1.数据库环境 创建⼀个存放书籍数据的数据库表
CREATE DATABASE `ssmbuild`;
USE `ssmbuild`;
DROP TABLE IF EXISTS `books`;
jsp开发环境CREATE TABLE `books` (
`bookID` INT(10) NOT NULL AUTO_INCREMENT COMMENT '书id',
`bookName` VARCHAR(100) NOT NULL COMMENT '书名',
`bookCounts` INT(11) NOT NULL COMMENT '数量',
`detail` VARCHAR(200) NOT NULL COMMENT '描述',
KEY `bookID` (`bookID`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES
(1,'Java',1,'从⼊门到放弃'),
(2,'MySQL',10,'从删库到跑路'),
(3,'Linux',5,'从进门到进牢');
2.基本环境搭建
新建⼀Maven项⽬!ssmbuild ,添加web的⽀持
导⼊相关的pom依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<!-- mvnrepository/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<!-- mvnrepository/artifact/com./jackson-databind -->
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<!-- mvnrepository/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<!-- mvnrepository/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.12</version>
</dependency>
<!-- mvnrepository/batis/mybatis-spring -->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId&hange</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>
</dependency>
<!-- mvnrepository/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<!-- mvnrepository/artifact/javax.servlet.jsp/jsp-api -->
<!-- mvnrepository/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- mvnrepository/artifact/org.apache.taglibs/taglibs-standard-impl -->
<!-- mvnrepository/artifact/taglibs/standard -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- mvnrepository/artifact/javax.servlet.jsp.jstl/jstl -->
<!-- mvnrepository/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
3.maven资源解析
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
4.基本结构和配置框架
5.配置l(只需配置设置和别名即可,其他使⽤spring的数据源替换Mybatis的配置)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration> <!--核⼼配置⽂件--> <!--在xml中所有标签都要规定其顺序-->
<!--1引⼊web配置⽂件-->
<!--2设置-->
<!--开启驼峰命名映射是否开启驼峰命名⾃动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。 --> <settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--显⽰开启全局缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<typeAliases>
<typeAlias type="com.ji.pojo.Books"/>
</typeAliases>
<!--每⼀个l都需要在mybatis核⼼配置⽂件中注册-->
<!--使⽤注解时不需要绑定配置⽂件,需要绑定接⼝-->
<mappers>
<package name="com.ji.dao"/>
</mappers>
</configuration>
5.编写pojo层 实体类
package com.ji.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private int bookID;
private String bookName;
private int bookCounts;
private String detail;
}
6.编写dao层(操作数据库) Mapper接⼝
package com.ji.dao;
import com.ji.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BooksMapper {
//增加
int addBook(Books books);
//删除
int deleteBooks(@Param ("bookID") int id);
//更改
int updateBooks(Books books);
//查询⼀本书
Books queryBooks(@Param ("bookID") int id);
List<Books> queryAllBooks();
List<Books> queryBookByName(String bookName);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ji.dao.BooksMapper">
<insert id="addBook" parameterType="Books">
insert into `books`(`bookName`,`bookCounts`,`detail`)
values (#{bookName},#{bookCounts},#{detail})
</insert>
<delete id="deleteBooks" parameterType="int">
delete from `books`
<where>
`bookID`=#{bookID}
</where>
</delete>
<update id="updateBooks" parameterType="Books">
update `books`
<set>
`bookName`=#{bookName},`bookCounts`=#{bookCounts},`detail`=#{detail}
</set>
<where>
`bookID`=#{bookID}
</where>
</update>
<select id="queryBooks" parameterType="int" resultType="Books">
select * from `books`
<where>
`bookID`=#{bookID}
</where>
</select>
<select id="queryBookByName" parameterType="String" resultType="Books">
select * from `books`
<where>
`bookName`=#{bookName}
</where>
</select>
<select id="queryAllBooks" resultType="Books">
select * from `books`
</select>
</mapper>
使⽤l代替l配置数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:c="/schema/c"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd">
<!--关联数据库配置⽂件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--连接池-->
<bean id="datasource" class="hange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="sql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="l"/>
</bean>
<!--配置dao接⼝扫描包,动态实现了dao接⼝可以注⼊到spring容器中⽆需继承实现类和私有化构造sqlSessionTemplate-->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<!--注⼊sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--扫描要扫描的包-->
<property name="basePackage" value="com.ji.dao"/>
</bean>
<!--sqlSessionTemplate 继承实现类-->
</beans>
7.编写service层 l配置⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:p="/schema/p"
xmlns:c="/schema/c"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/aop
/schema/aop/spring-aop.xsd
/schema/tx
/schema/mvc/tx.xsd">
<!--1.扫描service下的包-->
<context:component-scan base-package="com.ji.service"/>
<import resource="l"/>
<!--2.将我们的所有业务类注⼊到spring 可以通过配置-->
<bean id="BookServiceImpl" class="com.ji.service.BookServiceImpl">
<property name="booksMapper" ref="booksMapper"/>
</bean>
<!--3.声明式事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <!--注⼊数据源-->
<property name="dataSource" ref="datasource"/>
</bean>
<!--4.aop横切事务⽀持-->
<!--<tx:advice id="txAdvice" transaction-manager="transactionManager">
//给哪些⽅法配置事务
//配置事务的传播特性
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
//配置事务切⼊
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.ji.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>-->
</beans>
Bookservice层接⼝
package com.ji.service;
import com.ji.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookService {
/
/增加
int addBook(Books books);
//删除
int deleteBooks(int id);
//更改
int updateBooks(Books books);
//查询⼀本书
Books queryBooks(int id);
List<Books> queryAllBooks();
List<Books> queryBookByName(String bookName);
}
BookService实现类
package com.ji.service;
import com.ji.dao.BooksMapper;
import com.ji.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论