欢迎使用CSDN-markdown编辑器

mybatis+springboot+maven整合

1.使用eclipse开始创建maven项目这就不多说了。

2、简单项目结构

3、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>springBootM</groupId>
  <artifactId>testSpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>testSpringBoot</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version>
    <mysql-connector.version>5.1.39</mysql-connector.version>
  </properties>

   <!-- 继承父包 -->  
   <parent> 
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.3.3.RELEASE</version>
    </parent>  

    <!-- spring-boot的web启动的jar包 -->  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  


        <!-- start mybatis添加 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-actuator</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
        </dependency>  

        <!-- end -->

        <!--jpa的jar包 ,操作数据库的,类似hibernate-->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-data-jpa</artifactId>  
        </dependency>  

        <!--mysql驱动-->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
        </dependency>  

        <!--thymeleaf模板jar,是很不错的html数据传递取值,类似jsp的jstl-->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-thymeleaf</artifactId>  
        </dependency> 
    </dependencies>  

    <!--maven的插件-->  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  

    <!-- 配置java版本 不配置的话默认父类配置的是1.6-->  
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>1.7.0</source>
            <target>1.7.0</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>  
  </build>  

</project>

4、这里需要application.properties放在源码目录下:

spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://localhost/spring_mvc_test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=root
spring.datasource.password=ning
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# Advanced configuration...
spring.datasource.max-active=50
spring.datasource.max-idle=6
spring.datasource.min-idle=2
spring.datasource.initial-size=6

##create table
#spring.jpa.hibernate.ddl-auto=validate

#mybatis 
mybatis.typeAliasesPackage=com.xnw.demo.model
mybatis.mapperLocations=classpath:mapper/*.xml

server.port=1111
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8

# LOGGING  
logging:  
    level:com.ibatis:DEBUG


spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html 
spring.thymeleaf.cache=false

5、需要一个启动类

App.java其他的类名也可,放的位置参考上面的package com.xnw.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Hello world!
*
*/
@SpringBootApplication
@MapperScan(basePackages ={“com.xnw.demo”} )
public class App
{

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

}

6、其他代码块仅供参考

UserController 代码

package com.xnw.demo.controller;
import java.util.List;

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.ResponseBody;

import com.xnw.demo.model.UserModel;
import com.xnw.demo.service.UserServiceImpl;

@Controller
public class UserController {

@Autowired
private UserServiceImpl iUserService;

@RequestMapping(“login”)
@ResponseBody
public String login(){

List model = iUserService.getUser(“小黄”);
UserModel userModel = model.get(0);

return userModel.getUserCall()+”——-“+userModel.getUserName()+”下班了!!”;
}
}

UserServiceImpl代码

package com.xnw.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xnw.demo.dao.IUserDao;
import com.xnw.demo.model.UserModel;

@Service
public class UserServiceImpl{

@Autowired
private IUserDao iUserdao;

public List<UserModel> getUser(String userName) {

    return iUserdao.getUserAll(userName);
}

}

model代码块

package com.xnw.demo.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
* @author 999_x
*
*/
@Entity
public class UserModel {

private String userId;
private String userName;
private String userPassword;
private Date userCreatTime;
private String userCall;

@Id
public String getUserId() {
    return userId;
}
public void setUserId(String userId) {
    this.userId = userId;
}
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 Date getUserCreatTime() {
    return userCreatTime;
}
public void setUserCreatTime(Date userCreatTime) {
    this.userCreatTime = userCreatTime;
}
public String getUserCall() {
    return userCall;
}
public void setUserCall(String userCall) {
    this.userCall = userCall;
}

}

dao层代码块

package com.xnw.demo.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import com.xnw.demo.model.UserModel;

@Repository
public interface IUserDao {

List<UserModel> getUserAll(@Param("userName")String userName);

}
再加一个mapper.xml就好
现在就可以在app.java里run as一下运行Java application开跑了
出现这个样子就成功了
这里写图片描述

页面可以访问
这里写图片描述

成了吧!抓紧跑跑去吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值