Spring Boot

运行的主类

package com.gxj;



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


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

}


多模块:model、web、service。。。。。

在父层pom文件中导入依赖,管理jar包版本信息

web模块依赖service模块(因为web中包含controller需要调用service中的接口)

service模块依赖model模块(service中也需要model中的实体类,同时根据继承,则web模块中也会有model依赖)


测试中在service中直接进行数据库操作,则application.properties/mybatis.xml文件都放在service模块中

以下是application.properties文件源码:

server.port=9090      //启动时springboot内置tomcat端口号


jdbc.driver=com.mysql.jdbc.Driver     //数据库驱动
jdbc.url=jdbc\:mysql://localhost:3306/aaaa?useUnicode=true&characterEncoding=utf-8&useSSL=false   //数据库连接地址
jdbc.username=root     //username
jdbc.password=********//password


mybatis_config_file=mybatis-config.xml                  //mybatis的配置文件
mapper_path=/mapper/**.xml                                 //.xml的路径

entity_package=com.gxj.dao.StudentDao               //Dao层接口(有多个用英文逗号分隔)


以下是mybatis-config.xml    我把都注释了^_^

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
  <configuration>
  <!-- <settings> -->
  <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
  <!-- <setting name="userGeneratedKeys" value="true"/> -->
  <!-- 使用列标签替换列别名 默认:true -->
  <!-- <setting name="userColumnLabel" value="true"/> -->
  <!-- 开启驼峰命名转换Table{create_time} -> Entity{createTime} -->
  <!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
  <!-- </settings> -->

  </configuration> 

以下是jdbc数据源的配置

package com.gxj.config.dao;


import java.beans.PropertyVetoException;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
//配置mapper的扫描(并不懂为啥,先全包扫描吧)
@MapperScan("com.gxj")
public class DataSourcesConfig {
@Value("${jdbc.driver}")            //注意一定要写在${}中
private String jdbcDriver;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;

@Bean(name="dataSource")
public ComboPooledDataSource createDataSource() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUsername);
dataSource.setPassword(jdbcPassword);
//数据库连接关闭后不自动提交(commit)
dataSource.setAutoCommitOnClose(false);
return dataSource;
}

}

下面是创建sql工程

package com.gxj.config.dao;


import java.io.IOException;


import javax.sql.DataSource;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;


@Configuration
public class SessionFactoryConfig {
@Value("${mybatis_config_file}")
private String mybatisConfigFilePath;
@Value("${mapper_path}")
private String mapperPath;
@Value("${entity_package}")
private Class<?>[] entityPackage;
@Autowired
private DataSource dataSource;


@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliases(entityPackage);
return sqlSessionFactoryBean;
}

}

剩下的就和ssm框架一样一样的了^_^

打包的时候先clean install     然后再package


war包放到tomcat中访问不到controller。。。不懂(猜想是因为没有配置web.xml访问不到servlet)

通过cmd    java -jar xxxxxxxx.jar/xxxxxxx.war     都可以完美启动


差点忘了,还需要配置启动主类,我这块在web模块配置的pom.xml中(也就是那个App类)

  <build>
  <plugins>
  <plugin>
  <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.gxj.App</mainClass>
</configuration>
  </plugin>
  </plugins>
  </build>





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值