SSM整合
Posted jzfanqiejiang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合相关的知识,希望对你有一定的参考价值。
学完SSM就要整合一下嘛,
开始:
1、新建maven项目:
2、先导入springmvc的依赖包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
2.1再导入mybatis
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency>
3,创建包,分别为pojo,controller,service,mapper,utile这五个层,根据个人增加多少个无所谓
4,先在pojo层创建一个类作为mybatis的映射类名为UserPojo
package com.ssmtest.pojo; public class UserPojo { private int id; private String username; private String password; public UserPojo() { } public UserPojo(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserPojo{" + "id=" + id + ", username=‘" + username + ‘‘‘ + ", password=‘" + password + ‘‘‘ + ‘}‘; } }
5、mapper层:
UserMapper接口:
package com.ssmtest.mapper; import com.ssmtest.pojo.UserPojo; import org.apache.ibatis.annotations.Param; import java.util.List; public interface UserMapper { //增 int insertUser(UserPojo user); //删 int deletUser(@Param("userid") int id); //改 int updateUser(UserPojo user); //查 UserPojo selectUser(@Param("userid")int id); //查看全部 List<UserPojo> selectUserAll(); }
UserMapper.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.ssmtest.mapper.UserMapper"> <insert id="insertUser" parameterType="userpojo"> insert into mybatis.login_user(id,username, password) values (#{id},#{username},#{password}); </insert> <delete id="deletUser" parameterType="_int"> delete from mybatis.login_user where username = #{userid} </delete> <update id="updateUser" parameterType="userpojo"> update mybatis.user set name=#{username},pwd=#{password} where id=#{id}; </update> <select id="selectUser" parameterType="_int"> select * from mybatis.login_user where id=#{userid} </select> <select id="selectUserAll" resultType="userpojo"> select * from mybatis.login_user </select> </mapper>
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> <typeAliases> <package name="com.ssmtest.pojo"/> </typeAliases> <mappers> <mapper resource="com/ssmtest/mapper/UserMapper.xml"/> </mappers> </configuration>
5.1、先创建一个总的spring文件名为applicationContext:
注意一定要点Create new application context,要不然不能汇总,别的spring配置文件不能相互使用
5.2、创建db.properties数据库的配置文件
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&UseUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC user=自己的账号 pass=自己的密码
5.3、创建springmapper配置文件:
一定要加入到刚才的applicationContext里
配置文件:
<?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"> <!--读取db.properties数据库配置文件--> <context:property-placeholder location="classpath:db.properties"/> <!--开启注解--> <context:component-scan base-package="com.ssmtest.mapper"/> <!--设置MySQL连接池--> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${user}"/> <property name="password" value="${pass}"/> </bean> <!--这个就是mybatis里的SqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <!--绑定mybatis配置文件--> <property name="configLocation" value="classpath:Mybatisconfig.xml"/> </bean> <!--自动扫描 将Mapper接口生成代理注入到Spring--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssmtest.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
6,配置service层:
创建UserService接口:
package com.ssmtest.service; import com.ssmtest.pojo.UserPojo; import java.util.List; public interface UserService { //增 int insertUser(UserPojo user); //删 int deletUser(int id); //改 int updateUser(UserPojo user); //查 UserPojo selectUser(int id); //查看全部 List<UserPojo> selectUserAll(); }
实现UserService接口:
package com.ssmtest.service; import com.ssmtest.mapper.UserMapper; import com.ssmtest.pojo.UserPojo; import java.util.List; public class UserServicelmpl implements UserService { //调用mapper层 private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } public int insertUser(UserPojo user) { return userMapper.insertUser(user); } public int deletUser(int id) { return userMapper.deletUser(id); } public int updateUser(UserPojo user) { return userMapper.updateUser(user); } public UserPojo selectUser(int id) { return userMapper.selectUser(id); } public List<UserPojo> selectUserAll() { return userMapper.selectUserAll(); } }
配置service层的spring配置文件:
<?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.ssmtest.service"/> <bean class="com.ssmtest.service.UserServicelmpl" id="UserServicelmpl"> <!--重点把usermapper从springmapper.xml里拿到usermapper接口的实现类注入到service层里面--> <property name="userMapper" ref="userMapper"/> </bean> </beans>
测试一下,看一下spring整合mybatis是否成功:
先在applicationContext.xml中导入springservice和springmapper文件:
测试:
结果:
查出来了,说明spring整合mybatis成功,就剩下整合springmvc了
7,Controller层:
先把项目改查web项目:
配置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"> <servlet> <servlet-name>SSMTEST</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--绑定applicationContext配置文件--> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--拦截请求--> <servlet-mapping> <servlet-name>SSMTEST</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
新建springmvc配置文件:
<?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:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.ssmtest.controller"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> </beans>
在配置UserController类
package com.ssmtest.controller; import com.ssmtest.pojo.UserPojo; import com.ssmtest.service.UserService; 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 public class UserController { //调用service层 @Autowired @Qualifier("UserServicelmpl") private UserService userService; @RequestMapping("/hello") public String hello(Model model){ List<UserPojo> userPojos = userService.selectUserAll(); model.addAttribute("hello",userPojos); return "hello"; } }
在idea中添加lib,springmvc支持的jar包这样才能启动成功:
测试:
成功!
以上是关于SSM整合的主要内容,如果未能解决你的问题,请参考以下文章
520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“