最近自学了springboot,简单的玩了下,写了个简单的demo,供初学的朋友参考有不正确的地方还请指出。
1.创建项目,开发工具是IDEA,File->New Project(低版本的idea是没有这个选项的。15版以后就有了。)
然后finish就好了。
2.配置好Maven。
File->Settings
3.数据库配置
application.properties文件里面加上你的数据库配置。
application.properties
spring.datasource.primary.url=jdbc:oracle:thin:@ip:1521/服务名 spring.datasource.primary.username= spring.datasource.primary.password= spring.datasource.primary.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.primary.min-idle=5 spring.datasource.primary.max-active= 10 spring.datasource.primary.max-idle=10
4.配置pom.xml文件
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.dql</groupId> <artifactId>springboot-mybatis-oracle-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot-mybatis-oracle-demo</name> <description>springboot-mybatis-oracle-demo</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <ojdbc6.version>11.2.0.2.0</ojdbc6.version> <mail.version>1.4</mail.version> <java.version>1.7</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <!-- oracle --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>${ojdbc6.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
5.建立对应的目录结构,根据自己的项目规范来。
6.创建读取数据库配置Java文件DataSourceConfit.java
DataSourceConfig.java
@Configuration
@MapperScan(basePackages = "com.dql.mapper", sqlSessionTemplateRef = "primarySqlSessionTemplate")
@EnableTransactionManagement public class DataSourceConfig { @Bean(name = "primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public javax.sql.DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "primarySqlSessionFactory") public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") javax.sql.DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.dql.domain");//扫描entity包 使用别名 org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); configuration.setUseGeneratedKeys(true);//使用jdbc的getGeneratedKeys获取数据库自增主键值 configuration.setUseColumnLabel(true);//使用列别名替换列名 configuration.setMapUnderscoreToCamelCase(true);//-下划线字段名转换骆驼属性名 configuration.setCallSettersOnNulls(true);//解决空值映射问题 bean.setConfiguration(configuration); bean.setFailFast(true); bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml")); return bean.getObject(); } @Bean(name = "primaryTransactionManager") public DataSourceTransactionManager primaryTransactionManager( @Qualifier("primaryDataSource") javax.sql.DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "primarySqlSessionTemplate") public SqlSessionTemplate primarySqlSessionTemplate( @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
7.其他的就是增删改查了。
SysUserController.java
@RestController public class SysUserController { @Autowired private SysUserService sysUserService; @RequestMapping("/findAdminUser") public SysUserVo findAdminUser(){ return sysUserService.findAdminUser(); } }
SysUserVo.java
public class SysUserVo implements Serializable{ private Long id; //ID private String userName; //用户姓名 private String loginName; //登录名 private String password; //登陆密码 private String phone; //手机 private String email; //邮箱 private String userStatus; //用户状态 private Long createUser; //创建人 private Date createTime; //创建时间 private Long updateUser; //更新人 private Date updateTime; //更新时间 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUserStatus() { return userStatus; } public void setUserStatus(String userStatus) { this.userStatus = userStatus; } public Long getCreateUser() { return createUser; } public void setCreateUser(Long createUser) { this.createUser = createUser; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Long getUpdateUser() { return updateUser; } public void setUpdateUser(Long updateUser) { this.updateUser = updateUser; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }
SysUserMapper.java
@Mapper public interface SysUserMapper { SysUserVo findAdminUser(); }
SpringbootMybatisOracleDemoApplication.java
@SpringBootApplication public class SpringbootMybatisOracleDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisOracleDemoApplication.class, args); } }
SysUserMapper.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.dql.mapper.SysUserMapper"> <select id="findAdminUser" resultType="com.dql.domain.vo.SysUserVo"> SELECT * FROM T_SYS_USER su WHERE su.login_name = 'admin' </select> </mapper>
end