文章目录
- 1. 项目创建
- 2. spring
- (1) 导包
- (2) 配置类
- 3. mybatis
- (1) maven导包
- (2) mybatis配置文件
- (3) 连接配置文件
- (4) mapper映射文件
- (5) 在spring配置类中注册sqlsession的bean
- springMVC
- (1) maven导包
- (2) springMVC配置类
- (3) 初始化类
- 5. 测试
- (1) 创建3层架构
- (2) 编写Controller层
- service层
- mapper(dao)层
- tomcat部署启动
1. 项目创建
创建一个空maven项目:
2. spring
(1) 导包
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.22</version></dependency>
(2) 配置类
@Configuration
public class SpringConfig {}
3. mybatis
(1) maven导包
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.13</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.23</version></dependency>
(2) mybatis配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><properties resource="jdbc.properties"></properties><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><!--指定mapper映射文件在哪--><mapper resource=""/></mappers>
</configuration>
(3) 连接配置文件
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/stu
username = root
password = xjy1234
(4) mapper映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xjy.Mapper"></mapper>
(5) 在spring配置类中注册sqlsession的bean
@Beanpublic SqlSession getSqlSession() throws IOException {return new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("MybatisConfig.xml")).openSession();}
springMVC
(1) maven导包
<!--需要注释掉上面引入的spring-context,不然可能会引起版本冲突(spring-webmvc已经含有spring-context)--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>6.0.11</version></dependency><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>5.0.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.3</version></dependency>
(2) springMVC配置类
@Configuration
@EnableWebMvc
public class MvcConfig{
}
(3) 初始化类
public class servletInit extends AbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[]{SpringConfig.class};}@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[]{MvcConfig.class};}@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}
}
5. 测试
(1) 创建3层架构
分别在spring配置类和mvc配置类中添加包扫描:
@ComponentScan({"com.xjy.service"}) // spring包扫描
@ComponentScan("com.xjy.controller")//mvc包扫描
(2) 编写Controller层
@RestController
@RequestMapping("/user")
public class userController {@Autowiredprivate UserService userService;@GetMapping("/{id}")@ResponseBodypublic String getUserName(@PathVariable int id) throws IOException {System.out.println("接受到请求");String name = userService.getUserName(id);System.out.println("controller接受到返回结果"+name);return name;}
}
service层
接口:
package com.xjy.service;public interface UserService {String getUserName(int id);
}
实现类:
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate SqlSession session;@Overridepublic String getUserName(int id) throws IOException {UserMapper mapper = session.getMapper(UserMapper.class);String name = mapper.getUserName(id);return name;}
}
mapper(dao)层
接口:
public interface UserMapper {String getUserName(int id);
}
sql映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xjy.mapper.UserMapper"><select id="getUserName" resultType="java.lang.String">select name from user where id = #{id}</select>
</mapper>
tomcat部署启动
打包方式修改为war:
访问路劲修改为/:
启动tomcat服务,用postman进行访问:
成功接收: