JAVA - SpringBoot項目引用MyBatis操作數據庫
添加POM依賴:
javaspring框架。org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
Python web框架。mysql
mysql-connector-java
8.0.19
javaweb項目連接MySQL數據庫,創建Service相關文件,controller相關文件。
接口文件:orderservice
packagecom.example.recordboot.service;importcom.example.recordboot.entity.TblOrder;importjava.util.List;public interfaceOrderService {publicTblOrder GetModel();
spring連接數據庫、}
服務文件:orderserviceImpl
packagecom.example.recordboot.service;importcom.example.recordboot.dao.TblOrderMapper;importcom.example.recordboot.entity.TblOrder;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjavax.annotation.Resource;
@Servicepublic class OrderServiceImpl implementsOrderService {
@ResourceprivateTblOrderMapper tblOrderMapper;
@OverridepublicTblOrder GetModel() {
TblOrder res= tblOrderMapper.selectByPrimaryKey(50);returnres;
}
}
controller文件:
packagecom.example.recordboot.controller;importcom.example.recordboot.entity.TblOrder;importcom.example.recordboot.service.OrderService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/Order")public classOrderController {
@AutowiredprivateOrderService orderService;
@RequestMapping("/GetModel")publicTblOrder GetModel() {
TblOrder res=orderService.GetModel();returnres;
}
}
RestController是responsebody+Controller兩個注解的合體,一般就拿來直接傳json數據。? 為什么可以直接傳個對象過去呢?這是因為springboot內置了jackson模塊,可以在maven的依賴下看到這方面的jar包
簡單的配置與設置:
好現在講講設置,這里會想到,那些Controller啊,@Service啊還有MyBatis的注解@Mapper什么的spring怎么知道在哪呢?不用像mvc那樣搞個掃描設置嗎?
是的要的,這些我們在啟動類做了簡單的設置:
packagecom.example.recordboot;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages= "com.example.recordboot.dao")//這個注解注意一下 放DAO層的包名 對這個包下進行注入
public classRecordbootApplication {public static voidmain(String[] args) {
SpringApplication.run(RecordbootApplication.class, args);
}
}
application.properties?配置文件
#設置端口號
server.port=8095spring.devtools.restart.enabled=truespring.devtools.restart.additional-paths=src/main/java
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456#mybatis配置
#首先是實體類所在的包的名字
mybatis.type-aliases-package=com.example.recordboot.entity
mybatis.mapper-locations=classpath:mapper/*.xml
#mybatis使用resources的xml來映射數據庫表,這里就是resources下的mapper包的所有xml文件
dao?文件
packagecom.example.recordboot.dao;importcom.example.recordboot.entity.TblOrder;public interfaceTblOrderMapper {intdeleteByPrimaryKey(Integer id);intinsert(TblOrder record);intinsertSelective(TblOrder record);
TblOrder selectByPrimaryKey(Integer id);intupdateByPrimaryKeySelective(TblOrder record);intupdateByPrimaryKey(TblOrder record);
}
entity?文件
packagecom.example.recordboot.entity;importjava.util.Date;public classTblOrder {privateInteger id;privateString orderCode;privateInteger userId;privateInteger amount;privateDate uptime;privateString text;publicInteger getId() {returnid;
}public voidsetId(Integer id) {this.id =id;
}publicString getOrderCode() {returnorderCode;
}public voidsetOrderCode(String orderCode) {this.orderCode =orderCode;
}publicInteger getUserId() {returnuserId;
}public voidsetUserId(Integer userId) {this.userId =userId;
}publicInteger getAmount() {returnamount;
}public voidsetAmount(Integer amount) {this.amount =amount;
}publicDate getUptime() {returnuptime;
}public voidsetUptime(Date uptime) {this.uptime =uptime;
}publicString getText() {returntext;
}public voidsetText(String text) {this.text =text;
}
}
mapper文件
id, order_code, user_id, amount, uptime, text
selectfrom tbl_order
where id= #{id,jdbcType=INTEGER}
delete from tbl_order
where id= #{id,jdbcType=INTEGER}
SELECT LAST_INSERT_ID()insert into tbl_order (order_code, user_id, amount,
uptime, text)
values (#{orderCode,jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER}, #{amount,jdbcType=INTEGER},
#{uptime,jdbcType=TIMESTAMP}, #{text,jdbcType=VARCHAR})
SELECT LAST_INSERT_ID()insert into tbl_order
order_code,
user_id,
amount,
uptime,
text,
#{orderCode,jdbcType=VARCHAR},
#{userId,jdbcType=INTEGER},
#{amount,jdbcType=INTEGER},
#{uptime,jdbcType=TIMESTAMP},
#{text,jdbcType=VARCHAR},
update tbl_order
order_code= #{orderCode,jdbcType=VARCHAR},
user_id= #{userId,jdbcType=INTEGER},
amount= #{amount,jdbcType=INTEGER},
uptime= #{uptime,jdbcType=TIMESTAMP},
text= #{text,jdbcType=VARCHAR},
where id= #{id,jdbcType=INTEGER}
update tbl_order
set order_code= #{orderCode,jdbcType=VARCHAR},
user_id= #{userId,jdbcType=INTEGER},
amount= #{amount,jdbcType=INTEGER},
uptime= #{uptime,jdbcType=TIMESTAMP},
text= #{text,jdbcType=VARCHAR}
where id= #{id,jdbcType=INTEGER}
generatorConfig.xml?文件
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
完成。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态