1:jdbc连接(不是用配置文件)
package com.springboot.first.jdbcUtil;
import org.springframework.beans.factory.annotation.Value;
import java.sql.*;
public class JdbcUtil {
private static String DRIVER = “com.mysql.jdbc.Driver”;
private static String USERNAME = “root”;
private static String PASSWORD = “1234”;
private static String URL = “jdbc:mysql://localhost:3306/USER”;
private static Connection connection ;
/**
*
* Describe:获得数据库的连接
* @Author: wwh
* @Date: 2018/10/29 13:05
* @Param: []
* @Return: java.sql.Connection
*/
public static Connection getConnection(){
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
/**
*
* Describe:关闭数据库操作
* @Author: wwh
* @Date: 2018/10/29 13:06
* @Param: [connection, preparedStatement, resultSet]
* @Return: void
*/
public static void close(Connection connection, PreparedStatement preparedStatement,
ResultSet resultSet){
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null ){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2:使用JdbcTemplate
1.application.yml配置如下
2.Controller代码
package com.springboot.first.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RestController
@RequestMapping("/mydb")
public class JdbcTemplateCintroller {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/getUsers")
public List<Map<String,Object>> getDbType(){
String sql = "SELECT * FROM USER";
List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
for(Map<String,Object> map : list){
Set<Map.Entry<String, Object>> entries = map.entrySet( );
if(entries != null) {
Iterator<Map.Entry<String, Object>> iterator = entries.iterator( );
while(iterator.hasNext( )) {
Map.Entry<String, Object> entry = iterator.next( );
Object key = entry.getKey( );
Object value = entry.getValue();
System.out.println(key+":"+value);
}
}
}
return list;
}
}
3:SpringBoot+MyBatis
yml配置文件
mapper文件存放在resources文件夹下的mapper文件夹下。这里是通过yml配置文件中指定了mapper的存放位置。springboot自动加载
<?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.wwh.demo.storeBase.assistant.dao.AssistantDao">
<select id="queryObject" resultType="com.wwh.demo.storeBase.assistant.entity.Assistant" parameterType="string">
select *
from assistant
where assistant_name = #{assistantName}
</select>
id:对应着AssistantDao中的方法名称
resultType:返回类型
parameterType:传入的参数类型
public interface AssistantDao {
Assistant queryObject(String assistantName);
}
public interface AssistantService {
Assistant queryAssistant(String assistantName);
}
@Service
public class AssistantServiceImpl implements AssistantService {
@Autowired
private AssistantDao assistantDao;
@Override
public Assistant queryAssistant(String assistantName) {
return assistantDao.queryObject(assistantName);
}
}
最后controller中注入一下AssistantService,然后调用方法就可以了
Assistant assistant = assistantService.queryAssistant(assistantName);