`

Maven + Spring Boot + Mybatis 框架整合

阅读更多

整合过程介绍(开发工具:IntelliJ IDEA 

项目结构如下:



 

1、新建springboot maven项目,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.xieke.test</groupId>
    <artifactId>springbootmybatisdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springbootmybatisdemo</name>
    <description>Demo project for Spring Boot Mybatis</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.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.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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>

 2、添加application.properties文件到 resources下,配置如下:

server:
    port: 1314

#配置数据源
spring:
    datasource:
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
        username: root
        password: 999999
        driver-class-name: com.mysql.jdbc.Driver

#指定mybatis映射文件的地址
mybatis:
    mapper-locations: classpath:mapper/*.xml

# 打印sql
logging:
    level:
        com.xieke.test.dao : debug

 3、SQL初始化文件table.sql,内容如下:

use test;
drop table `user`;
create table `user` (
   `id` int(10) NOT NULL AUTO_INCREMENT,
   `name` varchar(25) NOT NULL,
   `password` varchar(25) NOT NULL,
   PRIMARY KEY (`id`)
 );
insert into `user` (`id`, `name`, `password`) values('1','xieke','999999');
insert into `user` (`id`, `name`, `password`) values('2','43434','324233');
insert into `user` (`id`, `name`, `password`) values('3','34344','343434');

4、控制层、业务层、持久层、实体类代码如下:

package com.xieke.test.controller;

import com.xieke.test.entity.User;
import com.xieke.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/show")
    @ResponseBody
    public String show(@RequestParam(value = "name")String name){
        User user = userService.findUserByName(name);
        if(null != user)
            return user.getId()+"/"+user.getName()+"/"+user.getPassword();
        else
            return "null";
    }

    @RequestMapping(value = "/show2")
    @ResponseBody
    public String show2(@RequestParam(value = "name")String name, @RequestParam(value = "password")String password){
        User user = userService.findUserByNameAndPassword(name, password);
        if(null != user)
            return user.getId()+"/"+user.getName()+"/"+user.getPassword();
        else
            return "null";
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public String add(){
        userService.saveUser();
        User user = userService.findUserByName("a123456789");
        if(null != user)
            return user.getId()+"/"+user.getName()+"/"+user.getPassword();
        else
            return "null";
    }

}

  

package com.xieke.test.service;

import com.xieke.test.dao.UserDao;
import com.xieke.test.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public User findUserByName(String name){
        User user = null;
        try{
            user = userDao.findUserByName(name);
        } catch (Exception e){

        }
        return user;
    }

    public User findUserByNameAndPassword(String name, String  password){
        User user = null;
        try{
            User u = new User();
            u.setName(name);
            u.setPassword(password);
            user = userDao.findUserByNameAndPassword(u);
        } catch (Exception e){

        }
        return user;
    }

    @Transactional
    public void saveUser ( ){
        userDao.insertUser("a123456789", "123456789");
        // int n = 1/0;
        // userDao.insertUser("b123456789", "123456789");
    }
}

 

package com.xieke.test.dao;

import com.xieke.test.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserDao {
    @Select("SELECT * FROM user WHERE name = #{name}")
    User findUserByName(@Param("name") String name);

    @Insert("INSERT INTO user(name, password) VALUES(#{name}, #{password})")
    int insertUser(@Param("name") String name, @Param("password") String password);

    User findUserByNameAndPassword (User  user);
}

 

package com.xieke.test.entity;

public class User {
    private long id;

    private String name;

    private String password;

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

 

5、启动类代码如下,启动后访问localhost:1314/user/show?name=xieke测试,详细代码请参考码云

 

package com.xieke.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
public class SpringbootmybatisdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootmybatisdemoApplication.class, args);
    }

}

 

   转载请注明出处:http://xieke90.iteye.com/blog/2423136

  • 大小: 47.4 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics