idea+maven+springboot+mybatis

确认maven环境,安装maven在这里就不赘述了.

在idea新建maven项目

 

 

打开pom文件,导入jar包

下面是配置之后的pom文件内容

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xzk</groupId>
<artifactId>testboot</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>testboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>testboot</finalName>
</build>
</project>

 maven刷新,确认导入包,

在main下新建java文件夹

 

 

新建入口类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
实体
public class User {
private String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
控制层
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@RequestMapping("/testboot")
public class TestBootController {
@RequestMapping("getuser")
public User getUser() {
User user = new User();
user.setName("test");
return user;
}
}

启动,

或者按alt+F12, 在路径后边加上 mvn spring-boot:run   回车

或者在cmd, 项目根目录下键入 mvn spring-boot:run

 集成mybatis

不用说,先配置pom文件,导入jar包,最终的pom内容:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xu</groupId>
<artifactId>testboot</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>testboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.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>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--支持使用 JDBC 访问数据库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!--Mysql / DataSource-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--Json Support-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.43</version>
</dependency>

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

接下来配置数据源,在resource下新建文件

application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.address=127.0.0.1
server.port=9090

#配置项目访问路径
#server.context-path=/test

在Application.class中配置DataSource,SqlSeesion. 最终的Application.class内容:

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;


//@EnableAutoConfiguration
//@Configuration
//@ComponentScan
@SpringBootApplication
@MapperScan("com.xu.boot.mapper")
public class Application {
private static Logger logger = Logger.getLogger(Application.class);

//DataSource配置
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return new DataSource();
}

//提供SqlSeesion
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));

return sqlSessionFactoryBean.getObject();
}

@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}

/**
* Main Start
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
logger.info("============= SpringBoot Start Success =============");
}

}

其中:

如果目录结构和springboot推荐的一样,那么只加@SpringBootApplication这一个注解就可以.

这个不用多说,mapper的映射路径

这个spring.datasource是和properties文件前缀匹配.

这个是xml的映射.

最终的目录结构:

这个yml文件是空的, 里边也可以配置内容,但一般我们都习惯在properties里面配置.

接下来是controller,service,mapper, 也可以用mybatis-generator自动生成

import com.alibaba.fastjson.JSONArray;
import com.xu.boot.model.User;
import com.xu.boot.service.UserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

private Logger logger = Logger.getLogger(UserController.class);

@Autowired
private UserService userService;

@RequestMapping("/getUserById")
@ResponseBody
public User getUserById(HttpServletRequest request) {
int id = Integer.parseInt(request.getParameter("id"));
User user = userService.getUserById(id);
if(user!=null){
logger.info(JSONArray.toJSON(user));
}
return user;
}
}

然后是service

import com.xu.boot.model.User;

public interface UserService {
User getUserById(int id);
}

-----------------------------------------

import com.xu.boot.mapper.UserMapper;
import com.xu.boot.model.User;
import com.xu.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

@Autowired
UserMapper mapper;

@Override
public User getUserById(int id) {
return mapper.getUserById(id);
}
}

--------------------mapper-----------------------

import com.xu.boot.model.User;

public interface UserMapper {
User getUserById(int id);
}

------------------------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.xu.boot.mapper.UserMapper">
<select id="getUserById" resultType="com.xu.boot.model.User" parameterType="java.lang.Integer">
select name, age,password from user WHERE id=#{id,jdbcType=INTEGER};
</select>

</mapper>

接下来启动Application, 运行main方法.在浏览器输入http://localhost:9090/getUserById?id=1

 

 

 


posted @ 2017-03-27 16:33  xzkt  阅读(...)  评论(...编辑  收藏