如何用IDEA搭建Springboot spring MVC Mybatis集成环境
程序员文章站
2022-03-10 19:55:50
...
1、首先通过IDEA新建一个工程,选择Spring Initialize,下一步,再勾选web ,Mybatis等最后完成
2、其中pom.xml内容的代码如下
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、application.properties内容如下:
spring.datasource.username=myapp
spring.datasource.password=myapp
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.entity
4、main方法启动类的代码如下:
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.dao")
@SpringBootApplication(scanBasePackages = {"com.example"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
需要注意的是如果在启动类不加scanBasePackages,则Spring 容器默认只扫描启动类所在包及其子包中的类并识别为Spring bean。如果需要扫描其他非启动类所在的包及子包的组件,需要手动指定包的路径。scanBasePackages = {"com.example","com.myproject"}
5、其他类中正常写即可,比如Controller代码如下 :
package com.example.controller;
import com.example.service.UserService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.sql.DataSource;
@RestController
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@Autowired
private DataSource dataSource;
@Autowired
private SqlSessionFactory sqlSessionFactory;
@RequestMapping("/index/{userName}")
public String getUser(@PathVariable("userName") String userName) {
System.out.println("userName:" + userName);
System.out.println(userService.getUserList(userName));
return "success";
}
}
6、dao接口代码如下:
package com.example.dao;
import com.example.entity.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList(String userName);
}
7、实体类代码如下:
package com.example.entity;
import java.io.Serializable;
import java.math.BigDecimal;
public class User implements Serializable {
private static final long serialVersionUID = -2248190721476487645L;
private String userName;
private String userPassword;
private BigDecimal age;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public BigDecimal getAge() {
return age;
}
public void setAge(BigDecimal age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", age=" + age +
'}';
}
}
8、业务逻辑类代码如下:
package com.example.service;
import com.example.dao.UserMapper;
import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getUserList(String userName) {
return userMapper.getUserList(userName);
}
}
9、mapper映射文件代码如下:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.UserMapper">
<select id="getUserList" parameterType="java.lang.String" resultMap="UserResultMap">
select * from user_info where user_name=#{userName}
</select>
<resultMap type="com.example.entity.User" id="UserResultMap">
<id property="userName" column="user_name"/>
<result property="userPassword" column="user_password"/>
<result property="age" column="age"/>
</resultMap>
</mapper>
10、数据库表结构如下(这里选用的是Oracle数据库):
create table user_info(
user_name varchar2(20) primary key,
user_password varchar2(20),
age int
)
insert all into user_info values('admin','123',23)
into user_info values('admin1','123',23)
into user_info values('admin2','123',23)
select 1 from dual;
--这里只是为了像MySQL那样,能一次性插入多条记录。所以采用了这种语法,其实也没方便多少。