建立存放書籍的數據庫java
CREATE DATABASE `ssmbuild`; USE `ssmbuild`; DROP TABLE IF EXISTS `books`; 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,'從進門到進牢');
以下圖:mysql
<dependencies> <!--aop--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectLombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--數據庫驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- 數據庫鏈接池 c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--Servlet - JSP - Jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.1.RELEASE</version> </dependency> </dependencies>
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
<?xml version="1.0" encoding="UTF-8"?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
<?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> </configuration>
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8 username=root password=123456
<?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.star.pojo"/> </typeAliases> <mappers> <mapper resource="com/star/dao/bookMapper.xml"/> </mappers> </configuration>
package com.star.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class books { private int bookID; private String booName; private int bookCounts; private String detail; }
package com.star.dao; import com.star.pojo.books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface bookMapper { // 添加一本書 public int addBook(books book); // 經過id刪除一本書 public int deleteBookByID(@Param("bookID") int id); // 修改一本書 public int updateBook(books books); // 查找全部的書 public List<books> queryAllBooks(); // 經過id查找一本書 public books queryBookById(@Param("bookID") int id); }
<?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.star.dao.bookMapper"> <insert id="addBook" parameterType="books"> insert into ssmbuild.books (bookName, bookCounts, detail) values (#{bookName},#{bookCounts},#{detail}); </insert> <delete id="deleteBookByID" parameterType="int"> delete from ssmbuild.books where bookID=#{bookID}; </delete> <update id="updateBook" parameterType="books"> update ssmbuild.books set bookName=#{bookName}, bookCounts=#{bookCounts}, detail=#{detail} where bookID=#{bookID}; </update> <select id="queryAllBooks" resultType="books"> select * from ssmbuild.books; </select> <select id="queryBookById" parameterType="int" resultType="books"> select * from ssmbuild.books where bookID=#{bookID}; </select> </mapper>
接口:web
package com.star.service; import com.star.pojo.books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface bookService { // 添加一本書 public int addBook(books book); // 經過id刪除一本書 public int deleteBookByID(@Param("bookID") int id); // 修改一本書 public int updateBook(books books); // 查找全部的書 public List<books> queryAllBooks(); // 經過id查找一本書 public books queryBookById(@Param("bookID") int id); }
實現類:spring
package com.star.service; import com.star.dao.bookMapper; import com.star.pojo.books; import java.util.List; public class bookServiceImpl implements bookService { private bookMapper bookMapper; public void setBookMapper(com.star.dao.bookMapper bookMapper) { this.bookMapper = bookMapper; } public int addBook(books book) { return bookMapper.addBook(book); } public int deleteBookByID(int id) { return bookMapper.deleteBookByID(id); } public int updateBook(books books) { return bookMapper.updateBook(books); } public List<books> queryAllBooks() { return bookMapper.queryAllBooks(); } public books queryBookById(int id) { return bookMapper.queryBookById(id); } }
OK!到這裏底層需求操做編寫完成;sql
<context:property-placeholder location="classpath:dataBase.properties"/>
<bean name="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}"/> <!-- c3p0鏈接池的私有屬性 --> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 關閉鏈接後自動提交事物 --> <property name="autoCommitOnClose" value="false"/> <!-- 獲取鏈接超時時間 --> <property name="checkoutTimeout" value="1000"/> <!-- 當獲取鏈接失敗重試次數 --> <property name="acquireIncrement" value="2"/> </bean>
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入數據庫鏈接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置MyBatis全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>
<bean name="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 給出須要掃描Dao接口包 --> <property name="basePackage" value="com.star.dao"/> </bean>
<bean id="bookServiceImpl" class="com.star.service.bookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入數據源--> <property name="dataSource" ref="dataSource"/> </bean> <!--aop事務支持--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--給哪些方法配置事務 配置事務的傳播特性--> <tx:attributes> <!--* 表示給全部的方法配置事務 REQUIRED 默認的傳播特性--> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <!--所用類的全部方法--> <aop:pointcut id="txPointcut" expression="execution(* com.star.dao.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--核心處理器--> <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:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--亂碼過濾器--> <filter> <filter-name>encoding</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-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Session過時時間--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://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.star.controller"/> <!--配置mvc註解驅動--> <mvc:annotation-driven/> <!--靜態資源默認配置--> <mvc:default-servlet-handler/> <!--視圖解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp"/> <property name="suffix" value=".jsp"/> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:spring-dao.xml"/> <import resource="classpath:spring-service.xml"/> <import resource="classpath:spring-mvc.xml"/> </beans>