SSM(Spring+SpringMVC+Mybatis)框架整合
程序员文章站
2022-06-04 18:42:40
1、数据准备 SET FOREIGN_KEY_CHECKS=0; -- -- Table structure for `admin` -- DROP TABLE IF EXISTS `admin`; CREATE TABLE `admin` ( `a_id` int(11) NOT NULL AUT ......
1、数据准备
set foreign_key_checks=0; -- ---------------------------- -- table structure for `admin` -- ---------------------------- drop table if exists `admin`; create table `admin` ( `a_id` int(11) not null auto_increment, `a_name` varchar(20) not null, `a_pwd` varchar(20) not null, primary key (`a_id`) ) engine=innodb default charset=utf8; -- ---------------------------- -- records of admin -- ---------------------------- -- ---------------------------- -- table structure for `book` -- ---------------------------- drop table if exists `book`; create table `book` ( `b_id` int(11) not null auto_increment, `b_isbn` varchar(20) not null, `b_name` varchar(40) not null, `b_author` varchar(20) not null, `b_cid` int(11) not null, `b_cover` varchar(50) not null, `b_publish_time` date not null, `b_remark` varchar(255) not null, `b_num` int(11) not null, primary key (`b_id`), key `b_cid` (`b_cid`), constraint `book_ibfk_1` foreign key (`b_cid`) references `category` (`c_id`) ) engine=innodb default charset=utf8; -- ---------------------------- -- records of book -- ---------------------------- -- ---------------------------- -- table structure for `category` -- ---------------------------- drop table if exists `category`; create table `category` ( `c_id` int(11) not null auto_increment, `c_name` varchar(20) not null, primary key (`c_id`) ) engine=innodb default charset=utf8; -- ---------------------------- -- records of category -- ---------------------------- -- ---------------------------- -- table structure for `log` -- ---------------------------- drop table if exists `log`; create table `log` ( `l_id` int(11) not null auto_increment, `l_uid` int(11) not null, `l_bid` int(11) not null, `l_begintime` date not null, `l_endtime` date not null, primary key (`l_id`), key `l_uid` (`l_uid`), key `l_bid` (`l_bid`), constraint `log_ibfk_1` foreign key (`l_uid`) references `user` (`u_id`), constraint `log_ibfk_2` foreign key (`l_bid`) references `book` (`b_id`) ) engine=innodb default charset=utf8; -- ---------------------------- -- records of log -- ---------------------------- -- ---------------------------- -- table structure for `user` -- ---------------------------- drop table if exists `user`; create table `user` ( `u_id` int(11) not null auto_increment, `u_name` varchar(20) not null, `u_pwd` varchar(20) not null, primary key (`u_id`) ) engine=innodb auto_increment=3 default charset=utf8; -- ---------------------------- -- records of user -- ---------------------------- insert into `user` values ('1', '鲁班', '1234567890'); insert into `user` values ('2', '杜甫', '123456');
2、新建项目
3、完善项目结构
4、导入所需jar包
<properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!--spring --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.3.13.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>4.3.13.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>4.3.13.release</version> </dependency> <!--mybatis --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.4.5</version> </dependency> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.3.1</version> </dependency> <!--junit --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> </dependency> <!--log4j --> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> <!--mysql连接驱动 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.43</version> </dependency> <!--c3p0连接池--> <dependency> <groupid>c3p0</groupid> <artifactid>c3p0</artifactid> <version>0.9.1.2</version> </dependency> <!--jsp--> <dependency> <groupid>javax.servlet</groupid> <artifactid>jsp-api</artifactid> <version>2.0</version> </dependency> <!--jstl--> <dependency> <groupid>javax.servlet.jsp.jstl</groupid> <artifactid>jstl-api</artifactid> <version>1.2</version> </dependency> <!--servlet--> <dependency> <groupid>javax.servlet</groupid> <artifactid>servlet-api</artifactid> <version>3.0</version> </dependency> <!--lombok--> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version>1.16.18</version> </dependency> </dependencies>
5、编写实体类
user:
package com.ssm.pojo; import lombok.allargsconstructor; import lombok.data; import lombok.noargsconstructor; import java.io.serializable; @data @allargsconstructor @noargsconstructor public class user implements serializable { private integer id; private string name; private string pwd; }
6、编写dao层
userdao:
package com.ssm.dao; import com.ssm.pojo.user; import org.springframework.stereotype.repository; import java.util.list; @repository("userdao") public interface userdao { void adduser(user user); void deleteuser(integer id); void updateinfo(user user); list<user> queryall(); user querybyid(integer id); }
7、根据dao层编写xml映射配置文件
usermapper.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.ssm.dao.userdao"> <resultmap id="usermapper" type="user"> <id property="id" column="u_id"/> <result property="name" column="u_name"/> <result property="pwd" column="u_pwd"/> </resultmap> <insert id="adduser" parametertype="user"> insert into user(u_name,u_pwd) values (#{name},#{pwd}) </insert> <select id="queryall" resultmap="usermapper"> select u_id,u_name,u_pwd from user </select> <select id="querybyid" resultmap="usermapper" parametertype="int"> select * from user where u_id = #{id} </select> <update id="updateinfo" parametertype="user"> update user set u_name = #{name},u_pwd = #{pwd} where u_id = #{id} </update> <delete id="deleteadmin" parametertype="int"> delete from user where u_id = #{id} </delete> </mapper>
8、编写mybatis主配置文件(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> <typealiases> <package name="com.ssm.pojo"/> </typealiases> <mappers> <mapper resource="mapper/usermapper.xml"/> </mappers> </configuration>
9、编写spring全局配置文件(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" 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"> <!--开启组件注解扫描--> <context:component-scan base-package="com.ssm"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller"/> </context:component-scan> <!--引入数据库配置文件--> <context:property-placeholder location="classpath:jdbc.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="sqlsessionfactorybean" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource"/> <property name="configlocation" value="classpath:mybatis-config.xml"/> </bean> <!--开启动态扫描dao层接口--> <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.ssm.dao"/> <property name="sqlsessionfactorybeanname" value="sqlsessionfactorybean"/> </bean> <!--事务--> <bean id="datasourcetransactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean> </beans>
10、编写service层
iuserservice:
package com.ssm.service; import com.ssm.pojo.user; import java.util.list; public interface iuserservice { void adduser(user user); void deleteuser(integer id); void updateinfo(user user); list<user> queryall(); user querybyid(integer id); }
userservice:
package com.ssm.service.impl; import com.ssm.dao.userdao; import com.ssm.pojo.user; import com.ssm.service.iuserservice; import org.springframework.stereotype.service; import javax.annotation.resource; import java.util.list; @service("userservice") public class userservice implements iuserservice { @resource private userdao userdao; @override public void adduser(user user) { userdao.adduser(user); } @override public void deleteuser(integer id) { userdao.deleteuser(id); } @override public void updateinfo(user user) { userdao.updateinfo(user); } @override public list<user> queryall() { return userdao.queryall(); } @override public user querybyid(integer id) { return userdao.querybyid(id); } }
11、spring整合mybatis完成(测试)
import com.ssm.pojo.user; import com.ssm.service.iuserservice; import org.junit.test; import org.junit.runner.runwith; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.springjunit4classrunner; import javax.annotation.resource; import java.util.list; @runwith(springjunit4classrunner.class) @contextconfiguration(locations = "classpath:applicationcontext.xml") public class usertest { @resource iuserservice userservice; @test public void getall() { list<user> users = userservice.queryall(); for (user user : users) { system.out.println(user); } } }
12、编写springmvc配置文件(springmvc.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: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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--资源过滤器--> <mvc:default-servlet-handler/> <!--注解驱动--> <mvc:annotation-driven/> <!--注解扫描--> <context:component-scan base-package="com.ssm.controller"/> <!--视图解析器--> <bean id="internalresourceviewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
13、编写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"> <display-name>archetype created web application</display-name> <!--配置spring监听器,默认只加载web-inf目录下的applicationcontext.xml配置文件--> <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> <!--配置dispatcherservlet前端控制器--> <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> </filter> <filter-mapping> <filter-name>characterencodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
14、编写controller层
usercontroller:
package com.ssm.controller; import com.ssm.pojo.user; import com.ssm.service.iuserservice; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import javax.annotation.resource; @requestmapping("/user") @controller public class usercontroller { @resource iuserservice userservice; @requestmapping("getuserinfo") public string test(model model) { user user = userservice.querybyid(1); model.addattribute("user", user); return "success"; } }
15、创建必要的测试页面
index.jsp:
<%@ page contenttype="text/html;charset=utf-8" language="java" %> <html> <head> <title>title</title> </head> <body> <a href="/user/getuserinfo">点我测试!</a> </body> </html>
success.jsp:
<%@ page contenttype="text/html;charset=utf-8" language="java" iselignored="false" %> <html> <head> <title>title</title> </head> <body> name:${user.name} password:${user.pwd} </body> </html>
16、整合完成,测试喽!
17、晒上项目结构图
18、其他配置文件
数据库配置文件:
jdbc.driver=com.mysql.jdbc.driver #数据库地址 jdbc.url=jdbc:mysql://localhost:3306/booksys?useunicode=true&characterencoding=utf8 #用户名 jdbc.username=root #密码 jdbc.password=123456 #最大连接数 c3p0.maxpoolsize=30 #最小连接数 c3p0.minpoolsize=10 #关闭连接后不自动commit c3p0.autocommitonclose=false #获取连接超时时间 c3p0.checkouttimeout=10000 #当获取连接失败重试次数 c3p0.acquireretryattempts=2
log4j日志配置文档:
log4j.rootlogger=debug,stdout, r log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.layout=org.apache.log4j.patternlayout # pattern to output the caller's file name and line number. log4j.appender.stdout.layout.conversionpattern=%5p [%t] (%f:%l) - %m%n log4j.appender.r=org.apache.log4j.rollingfileappender log4j.appender.r.file=example.log log4j.appender.r.maxfilesize=100kb # keep one backup file log4j.appender.r.maxbackupindex=5 log4j.appender.r.layout=org.apache.log4j.patternlayout log4j.appender.r.layout.conversionpattern=%p %t %c - %m%n
上一篇: 四川万源市土特产有哪些?不看不知道
下一篇: 红薯干热量是多少你真的知道吗?