学完SSM就要整合一下嘛,
开始:
1、新建maven项目:
2、先导入springmvc的依赖包
<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>5.2.2.RELEASEversion> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>4.0.1version> <scope>providedscope> dependency>
2.1再导入mybatis
<dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.5.3version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>2.0.2version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>5.2.2.RELEASEversion> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.19version> 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); //查看全部 ListselectUserAll(); }
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"> <context:property-placeholder location="classpath:db.properties"/> <context:component-scan base-package="com.ssmtest.mapper"/> <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> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:Mybatisconfig.xml"/> bean> <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); //查看全部 ListselectUserAll(); }
实现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 ListselectUserAll() { 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"> <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>SSMTESTservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:applicationContext.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>SSMTESTservlet-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){ ListuserPojos = userService.selectUserAll(); model.addAttribute("hello",userPojos); return "hello"; } }
在idea中添加lib,springmvc支持的jar包这样才能启动成功:
测试:
成功!