一、Mybatis层
1.搭建项目所需环境
- 创建数据库表结构
- 使用IDEA 创建普通的maven 项目
2.在Maven中添加依赖
- 其中包括:junit、数据库驱动、连接池、servlet、jsp、mybatis、mybatis-spring、spring
- 注意静态资源问题
<?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>com.gaoxu</groupId> <artifactId>smsbuild</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!--依赖 junit、数据库驱动、连接池、servlet、jsp、mybatis、mybatis-spring、spring--> <dependencies> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</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.0</version> <scope>provided</scope> </dependency> <!--mybatis--> <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.2</version> </dependency> <!--文件上传依赖的两个jar包--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <!--C3P0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</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> </project>
3.在IDEA中链接数据库
点击右侧DataBase
- 输入用户名密码,选择数据库
遇到如下问题:Server returns invalid timezone. Need to set 'serverTimezone' property. . (34 ms)
在mysql的命令窗口执行:set global time_zone='+8:00';
4.创建必要的配置文件和包
- 在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> </configuration>
- 在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: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 https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> </bean>
在rescourse下创建database.properties
jdbc.diver=com.mysql.cj.jdbc.Driver #如果使用的是mysql8.0以上要加入时区:serverTimezone=Asia/Shanghai jdbc.url=jdbc:mysql://localhost:3306/diary?serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=
创建实体类Books.java
<!--Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.18</version> </dependency>
使用Lombok 来快速构建getter setter 有参构造,无参构造
package com.gaoxu.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data//getter setter @AllArgsConstructor//有参构造 @NoArgsConstructor//无参构造 public class Books { private int bookID; private String bookName; private int bookCount; private String detail; }
5.完善mybatis-config.xml
- 在dao包中创建BookMapper.java的接口类,定义其中的增删改查接口
package com.gaoxu.dao; import com.gaoxu.pojo.Books; import java.util.List; public interface BookMapper { //增加 int addBook(Books book); //删除 int removeBook(int id); //修改 int updataBook(Books book); //查询 Books queryBook(int id); //All List<Books> queryAllBook(); }
- 在统计目录下新建BookMapper.xml的配置文件
<?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.gaoxu.dao.BookMapper"> <insert id="addBook" parameterType="Books"> insert into ssmbuild.books(bookID,bookName,bookCount,detail) values (#{bookID},#{bookName},#{bookCount},#{detail}) </insert> <delete id="removeBook" parameterType="int"> delete from ssmbuild.books where bookID = #{bookId} </delete> <update id="updataBook" parameterType="Books"> update ssmbuild.books set bookName = #{bookName},bookCount = #{bookCount},detail = #{detail} where bookID = #{bookID}; </update> <select id="queryBook" parameterType="Books"> select * from ssmbuild.books where bookID = #{bookID}; </select> <select id="queryAllBook" parameterType="Books"> select * from ssmbuild.books; </select> </mapper>
- 将写好的map配置文件绑定到mybatis的配置文件中
<?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> <!--配置数据源,交给spring处理--> <!--对一个类起别名--> <typeAliases> <package name="com.gaoxu.pojo"/> </typeAliases> <mappers> <mapper class="com.gaoxu.dao.BookMapper"></mapper> </mappers> </configuration>
6.创建BookService和BookImpl
- 创建BookService 接口类和BookMapper一样的
BookServiceImpl 实现了BookService ,同时设置私有属性 BookMapper 并设置其set方法,方便spring进行注入
package com.gaoxu.service; import com.gaoxu.dao.BookMapper; import com.gaoxu.pojo.Books; import java.util.List; public class BookServiceImpl implements BookService{ private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int addBook(Books book) { return bookMapper.addBook(book); } @Override public int removeBook(int id) { return bookMapper.removeBook(id); } @Override public int updataBook(Books book) { return bookMapper.updataBook(book); } @Override public Books queryBook(int id) { return bookMapper.queryBook(id); } @Override public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } }
二、Spring层
1.创建spring-dao.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"> <!--1.关联数据库配置文件--> <context:property-placeholder location="classpath:database.properties"/> <!--2.连接池 dhcp:半自动化,不能自动连接 c3p0:自动化链接,自动化加载配置文件,自动设置到对象中 druid hikari --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.diver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--3.sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--绑定Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--配置dao接口扫描包,动态实现Dao可以注入Spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.gaoxu.dao"/> </bean> </beans>
2.创建spring-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!--扫描service下的包--> <context:component-scan base-package="com.gaoxu.service"/> <!--将我们所有的业务量注入到spring或注解实现--> <bean id="BookServiceImpl" class="com.gaoxu.service.BookServiceImpl"> <property name="bookMapper" ref="bookMapper"/> </bean> <!--声明事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="dataSource"></property> </bean> <!--aop事务支持--> </beans>
三、SpringMVC层
1.将maven项目添加web支持
- 右键 勾选web
2.配置web.xml文件
<?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">
<!--DispatchServlet-->
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--乱码过滤-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--默认Session过期时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
3.运行报错
web配置文件
<servlet> <servlet-name>springmvc</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>