工具:IntelliJ IDEA 2017.2、Maven 3.3.9
一、建立简单项目
二、配置pom文件
在此之前要确保Maven设置路径正确
File-Settings-Build-Build Tools-Maven中的Maven home directory设置maven的根目录
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatis.version>3.4.6</mybatis.version>
<mybatis.spring.version>1.3.2</mybatis.spring.version>
<pagehelper.version>5.0.4</pagehelper.version>
<druid.version>1.1.9</druid.version>
</properties>
<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.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--Slf4j注解生成-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<dependencies>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
三、项目结构
generatorConfig.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--设置本地目录jar地址 -->
<classPathEntry location="D:/Maven_Repo/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3" defaultModelType="flat">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/jeffy"
userId="root"
password="111111">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- model 文件生成 -->
<javaModelGenerator targetPackage="com.example.jeffy.model" targetProject="D:/workspace/jeffy/src/main/java/">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper.xml 文件生成 -->
<sqlMapGenerator targetPackage="mapper" targetProject="D:/workspace/jeffy/src/main/resources/">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mapper 文件生成-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.jeffy.mapper" targetProject="D:/workspace/jeffy/src/main/java/">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table schema="joyxuandb" tableName="t_user" domainObjectName="UserInfo"
enableCountByExample="false"
enableSelectByExample="false"
enableDeleteByExample="false"
enableUpdateByExample="false"
>
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<!--<generatedKey column="id" sqlStatement="select nextVal('EXPENSES_RECORD_SEQ');"/>-->
</table>
</context>
</generatorConfiguration>
application.properties配置文件
#配置端口
server.port=8081
#连接数据库四要素
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jeffy
jdbc.username=root
jdbc.password=111111
#mybtais相关
mapper_path=/mapper/*.xml
entity_package=com.example.jeffy.model
Generator插件生成mapper接口,model对象,mapper.xml
四、Controller、Service
Controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public List<UserInfo> selectAll(){
return userService.selectAll();
}
}
ServiceImpl(Service接口自行建立)
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserInfoMapper userInfoMapper;
@Override
public List<UserInfo> selectAll() {
return userInfoMapper.selectAll();
}
}
Mapper(mapper接口自行建立)
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
</select>
五、配置类
DataSourceConfig
@Configuration
@MapperScan("com.example.jeffy.mapper")
public class DataSourceConfig {
/*获取配置数据库四要素*/
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.username}")
private String jdbcUser;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(name="dataSource")
public DruidDataSource createDataSource() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(jdbcUrl);
dataSource.setDriverClassName(jdbcDriver);
dataSource.setUsername(jdbcUser);
dataSource.setPassword(jdbcPassword);
dataSource.setDefaultAutoCommit(false);
return dataSource;
}
}
SqlSessionFactoryConfig
@Configuration
@Slf4j
public class SessionFactoryConfig {
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Value("${mapper_path}")
private String MAPPERPATH;
@Value("${entity_package}")
private String entityPackage;
@Bean(name = "SqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPERPATH);
sqlSessionFactoryBean.setMapperLocations(resources);
sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
}
六、进行测试
http://localhost:8081/user/list
ending 如有错误,欢迎指出,本人也只是渣渣一枚,就算简单的代码,也要手敲,切勿cv流~