JAVA - SpringBoot项目引用MyBatis操作数据库
添加POM依赖:
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
mysql
mysql-connector-java
8.0.19
创建Service相关文件,controller相关文件。
接口文件:orderservice
packagecom.example.recordboot.service;importcom.example.recordboot.entity.TblOrder;importjava.util.List;public interfaceOrderService {publicTblOrder GetModel();
}
服务文件: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">
完成。