目錄
1、j在pom文件中引入dbc驅(qū)動(dòng)與mybatis
?2、數(shù)據(jù)庫(kù)連接池參數(shù)配置
3、mybatis配置
4、application配置
5、通用mapper配置
6、編寫UserMapper
7、user表(需要自行去mysql創(chuàng)建數(shù)據(jù)庫(kù)表新增用戶數(shù)據(jù),sql語句見下一步)
8、sql信息
9、UserService編寫
10、測(cè)試結(jié)果
11、測(cè)試輸出結(jié)果
訪問效果
????????springboot連接數(shù)據(jù)庫(kù)需要整合jdbc與事務(wù),那么改怎么處理,答案是不需要我們處理,springboot已經(jīng)實(shí)現(xiàn),我們只需在pom文件中引入對(duì)應(yīng)的庫(kù)然后簡(jiǎn)單配置即可實(shí)現(xiàn)。jdbc驅(qū)動(dòng)的引入,別忘了還有mybatis引入。下面我們來實(shí)現(xiàn)下:
1、j在pom文件中引入dbc驅(qū)動(dòng)與mybatis
<!--jdbc事務(wù)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql 數(shù)據(jù)庫(kù)驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
?2、數(shù)據(jù)庫(kù)連接池參數(shù)配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mumu
username: "數(shù)據(jù)庫(kù)賬號(hào)"
password: "數(shù)據(jù)庫(kù)密碼"
3、mybatis配置
#mybatis
# mybatis配置
mybatis:
# 實(shí)體類別名包路徑
type-aliases-package: springbootdemo.pojo
# 映射文件路徑
# mapper-locations: classpath:mappers/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4、application配置
在啟動(dòng)類上添加掃描包注解(推薦): import tk.mybatis.spring.annotation.MapperScan;別導(dǎo)錯(cuò)包。
package springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("springbootdemo.mapper")
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
5、通用mapper配置
????????通用Mapper的作者也為自己的插件編寫了啟動(dòng)器,我們直接引入即可。在項(xiàng)目的 pom.xml 文件中加入如下依賴,注意:一旦引入了通用Mapper的啟動(dòng)器,會(huì)覆蓋Mybatis官方啟動(dòng)器的功能,因此需要移除對(duì)官方Mybatis啟動(dòng)器的依賴。文章來源:http://www.zghlxwxcb.cn/news/detail-610025.html
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
6、編寫UserMapper
package springbootdemo.mapper;
import springbootdemo.pojo.User;
import tk.mybatis.mapper.common.Mapper;
public interface UserMapper extends Mapper<User> {
}
7、user表(需要自行去mysql創(chuàng)建數(shù)據(jù)庫(kù)表新增用戶數(shù)據(jù),sql語句見下一步)
package springbootdemo.pojo;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Data
@Table(name = "tb_user")
public class User{
// id
@Id
//開啟主鍵自動(dòng)回填
@KeySql(useGeneratedKeys = true)
private Long id;
// 用戶名
private String userName;
// 密碼
private String password;
// 姓名
private String name;
// 年齡
private Integer age;
// 性別,1男性,2女性
private Integer sex;
// 出生日期
private Date birthday;
// 創(chuàng)建時(shí)間
private Date created;
// 更新時(shí)間
private Date updated;
// 備注
private String note;
}
8、sql信息
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) DEFAULT NULL COMMENT '用戶名',
`password` varchar(100) DEFAULT NULL COMMENT '密碼',
`name` varchar(100) DEFAULT NULL COMMENT '姓名',
`age` int(10) DEFAULT NULL COMMENT '年齡',
`sex` tinyint(1) DEFAULT NULL COMMENT '性別,1男性,2女性',
`birthday` date DEFAULT NULL COMMENT '出生日期',
`note` varchar(255) DEFAULT NULL COMMENT '備注',
`created` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
`updated` datetime DEFAULT NULL COMMENT '更新時(shí)間',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'sw', '123456', '千帆v', '30', '1', '1964-08-08', '李先生很棒', '2014-09-19 16:56:04', '2014-09-21 11:24:59');
INSERT INTO `tb_user` VALUES ('2', '34r', '123456', '李哇', '21', '2', '1995-01-01', '李李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('3', 'dsff', '123456', '王如何', '22', '2', '1994-01-01', '李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('4', 'ht', '123456', '張奧迪', '20', '1', '1996-09-01', '李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('5', 'grr', '123456', '李二', '28', '1', '1988-01-01', '李娜李先生很棒', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('6', 'rr', '123456', '李法人', '23', '1', '1993-08-08', '李雷李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('7', 'hh', '123456', '威威', '24', '2', '1992-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('8', 'hrr', '123456', '為如', '21', '2', '2008-07-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('9', 'kytt', '123456', '馬搭', '18', '2', '2012-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('10', 'gvr', '123456', '安安', '45', '2', '1971-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('11', 'htrh', '123456', 'de', '33', '2', '1983-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
INSERT INTO `tb_user` VALUES ('12', 'wre', '123456', '馬大哈', '46', '2', '1980-08-08', '李先生很棒', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
9、UserService編寫
package springbootdemo.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import springbootdemo.mapper.UserMapper;
import springbootdemo.pojo.User;
@Service
public class UserService {
@Autowired(required=false)
private UserMapper userMapper;
//根據(jù)id查詢
public User queryById(Long id) {
return userMapper.selectByPrimaryKey(id);
}
@Transactional
public void saveUser(User user) {
System.out.println("新增用戶...");
userMapper.insertSelective(user);
}
}
10、測(cè)試結(jié)果
package springbootdemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import springbootdemo.UserService.UserService;
import springbootdemo.pojo.User;
import javax.sql.DataSource;
@RestController
public class HelloController {
@Autowired
private UserService userService;
/**
* 根據(jù)id獲取用戶
* @param id 用戶id
* @return 用戶
*/
@GetMapping("/user/{id}")
public User queryById(@PathVariable Long id){
return userService.queryById(id);
}
}
11、測(cè)試輸出結(jié)果
瀏覽器輸入地址:http://localhost:8080/user/6文章來源地址http://www.zghlxwxcb.cn/news/detail-610025.html
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49e8af71] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@546085857 wrapping com.mysql.jdbc.JDBC4Connection@574e7689] will not be managed by Spring
==> Preparing: SELECT id,user_name,password,name,age,sex,birthday,created,updated,note FROM tb_user WHERE id = ?
==> Parameters: 6(Long)
<== Columns: id, user_name, password, name, age, sex, birthday, created, updated, note
<== Row: 6, lilei, 123456, 李雷, 23, 1, 1993-08-08, 2014-09-20 11:41:15.0, 2014-09-20 11:41:15.0, 李先生很棒
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49e8af71]
到了這里,關(guān)于spring boot 整合jdbc和事務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!