项目介绍:这个项目是使用maven多模块构建的
父pom文件
<?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.xiaofeng</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- 管理所有模块 -->
<modules>
<module>collect</module>
</modules>
<!-- springboot版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath />
</parent>
<!-- 设置编码及版本号 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--默认关掉单元测试 -->
<skipTests>true</skipTests>
<pagehelper.version>1.2.3</pagehelper.version>
<druid.version>1.0.29</druid.version>
<fastjson.version>1.2.31</fastjson.version>
<lombok.version>1.16.20</lombok.version>
<servlet.version>3.1.0</servlet.version>
<slf4j.version>1.7.25</slf4j.version>
</properties>
<!-- 版本管理 -->
<dependencyManagement>
<dependencies>
<!--pagehelper自动依赖mybatis -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- alibaba druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- alibaba fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- common层用到servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- common层用到slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
我在子模块collect里面整合mybatis
collect模块的pom文件
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xiaofeng</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>collect</artifactId>
<name>collect</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
整个项目的结构:
这个项目连接的数据库类型是mysql,连接的是collect数据库
在collect数据库中新建user_info表
CREATE TABLE `user_info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(120) DEFAULT NULL,
`alias` varchar(120) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
并新建collect数据库的用户mycollect,密码为mycollect,同时设置相应的权限
1、实体层
package com.xiaofeng.collect.entity;
public class UserInfo {
private Long id;
private String name;
private String alias;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
}
2、dao层
package com.xiaofeng.collect.dao;
import com.xiaofeng.collect.entity.UserInfo;
public interface UserInfoDao {
public int addUserInfo(UserInfo userInfo);
}
3、service层
package com.xiaofeng.collect.service;
import com.xiaofeng.collect.entity.UserInfo;
public interface UserInfoService {
public int addUserInfo(UserInfo userInfo);
}
4、serviceImpl层
package com.xiaofeng.collect.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiaofeng.collect.dao.UserInfoDao;
import com.xiaofeng.collect.entity.UserInfo;
import com.xiaofeng.collect.service.UserInfoService;
@Service
public class UserInfoServiceImpl implements UserInfoService{
@Autowired
private UserInfoDao userInfoDao;
@Override
public int addUserInfo(UserInfo userInfo) {
return userInfoDao.addUserInfo(userInfo);
}
}
5、启动类
package com.xiaofeng.collect;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages="com.xiaofeng.collect.service")
@MapperScan("com.xiaofeng.collect.dao")
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
6、UserInfoMapper.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.xiaofeng.collect.dao.UserInfoDao" >
<resultMap id="UserInfoMap" type="com.xiaofeng.collect.entity.UserInfo" >
<result column="id" property="id" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="alias" property="alias" jdbcType="VARCHAR" />
</resultMap>
<insert id="addUserInfo" parameterType="com.xiaofeng.collect.entity.UserInfo">
insert into user_info(id,name,alias)
values(#{id,jdbcType=BIGINT},#{name,jdbcType=VARCHAR},#{alias,jdbcType=VARCHAR})
</insert>
</mapper>
7、application.yml文件
server:
port: 7777
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/collect
username: mycollect
password: mycollect
driver-class-name: com.mysql.jdbc.Driver
# mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
8、测试类
package com.xiaofeng.collect;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.xiaofeng.collect.entity.UserInfo;
import com.xiaofeng.collect.service.UserInfoService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest
{
@Autowired
private UserInfoService userInfoService;
@Test
public void addUserInfoTest(){
UserInfo userInfo = new UserInfo();
userInfo.setName("xiaoLinFeng");
userInfo.setAlias("小林峰");
System.out.println(userInfoService.addUserInfo(userInfo));
}
}
验证结果: