SpringBoot配合mybatis、maven初始化工程

1、使用IDEA新建一个空的maven项目

SpringBoot配合mybatis、maven初始化工程_第1张图片

2、整个工程目录如下图所示:

SpringBoot配合mybatis、maven初始化工程_第2张图片

3、其中DemoApplocation.java为系统启动类,具体代码如下:

package com.cjs.example

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

@SpringBootApplication
@MapperScan("com.cjs.example.dao")
public class DemoApplication {
	public static void main(String[] args){
		SpringApplication.run(DemoApplication.class, args);
	}
}

4、HelloController.java代码如下:

package com.cjs.example.controller

import com.cjs.example.dto.UserDto;
import com.cjs.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class HelloController {
	@Autowired
	private UserService userService;
	@RequestMapping(value="/hello", method=RequestMethod.GET)
	public String hello(){return "hello world!";}
	@RequestMapping(value="/alluser", method=RequestMethod.GET)
	public List getAllUser(){return userService.selectAllUser();}
}

5、UserService以及UserServiceImpl代码如下:

package com.cjs.example.service
import com.cjs.example.dto.UserDto;
import java.util.List;

public interface UserService{
	List selectAllUser();
}
package com.cjs.example.service.impl;

import com.cjs.example.dao.UserDao;
import com.cjs.example.dto.UserDto;
import com.cjs.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserDao userDao;
	@Override
	public List selectAllUser(){
		return userDao.selectAlluser();
	}
}

6、UserDao代码如下:

package com.cjs.example.dao;

import com.cjs.example.dto.UserDto;
import org.springframework.stereotype.Repositort;
import java.util.List;

@Repository
public interface UserDao{
	List selectAllUser();
}

7、UserMapper.xml代码如下:





	
		
		
	
	

8、整个工程配置在application.properties中,配置如下

server.port:9999
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/systemdb?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mapper/*.xml
management.security.enabled=false

9、以及pom.xml配置相关如下:



	4.0.0

	com.cjs
	springboottest1
	0.0.1-SNAPSHOT
	jar

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.3.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		
		
			mysql
			mysql-connector-java
			5.1.25
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.apache.maven.plugins
				maven-complier-plugin
				
					1.8
					1.8
					UTF-8
					false
				
			
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					com.cjs.example.DemoApplication
				
				
					
						
							repackage
						
					
				
			
		
	




10、备注:actuator为监控与管理工具,存在以下链接可以访问:

应用配置类:
1、应用自动化配置报告:/autoconfig
2、上下文中创建的bean:/beans
3、应用中配置的属性信息报告:/configprops
4、应用所有可用的环境属性报告:/env
5、所有spring mvc的控制器映射关系报告:/mappings
6、返回应用自定义的信息:/info
度量指标类:
1、返回当前应用的各类重要度量指标,比如内存信息、线程信息、垃圾回收信息:/metrics

你可能感兴趣的:(网页前端后端)