【SpringBoot】又写了一份新瓶装旧酒的CRUD程序
SpringBoot版本:2.5.4
后台数据库:Oracle11g
访问数据库:MyBatis
例程下载:https://files.cnblogs.com/files/heyang78/redisCache_crud_oracle_mybatis_0925.rar
Pom.xml中配置:
<!-- Spring MyBatis Support --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- Oracle Support --> <dependency> <groupId>com.oracle.database.nls</groupId> <artifactId>orai18n</artifactId> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> </dependency>
application-dev.yml中数据库配置:
server: port: 8080 myenv: name: '开发环境' spring: datasource: url: jdbc:oracle:thin:@127.0.0.1:1521:orclhy username: luna password: 1234 driver-class-name: oracle.jdbc.OracleDriver
访问数据库的Mapper类:
import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; @Mapper public interface EmpMapper { @Select("select * from emp where id=#{id}") Emp findById(@Param("id") long id); @Insert("insert into emp(id,name,age) values(#{id},#{name},#{age})") int hireEmp(@Param("id") long id,@Param("name") String name,@Param("age") int age); @Delete("delete from emp where id=#{id}") int fireEmp(@Param("id") long id); @Update("Update emp set name=#{name},age=#{age} where id=#{id}") int modifyEmp(@Param("id") long id,@Param("name") String name,@Param("age") int age); @Select("select * from emp where name like #{name} ") List<Emp> searchByName(@Param("name") String name); }
和外界交互的控制器类:
package com.hy.myapp; import java.util.List; import javax.annotation.Resource; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class JsonCtrl { @Resource private EmpMapper eMper; /** * 按名称查找一堆雇员 * @param name * @return */ @RequestMapping(value="/searchEmpsByName", method=RequestMethod.GET) public List<Emp> searchEmpsByName(@ModelAttribute(value="name") String name) { List<Emp> emps=eMper.searchByName("%"+name+"%"); return emps; } /** * 更改一个雇员信息 * @param id * @param name * @param age * @return */ @RequestMapping(value="/modifyEmp", method=RequestMethod.POST) public String modifyEmp(@ModelAttribute(value="id") long id, @ModelAttribute(value="name") String name, @ModelAttribute(value="age") int age) { int retval=eMper.modifyEmp(id, name, age); if(retval==1) { return "successful"; }else { return "failed"; } } /** * 删除一个雇员 * @param id * @return */ @RequestMapping(value="/fireEmp", method=RequestMethod.DELETE) public String fireEmp(@ModelAttribute(value="id") long id) { int retval=eMper.fireEmp(id); if(retval==1) { return "successful"; }else { return "failed"; } } /** * 增加一个雇员 * @param id * @param name * @param age * @return */ @RequestMapping(value="/hireEmp", method=RequestMethod.POST) public String hireEmp(@ModelAttribute(value="id") long id, @ModelAttribute(value="name") String name, @ModelAttribute(value="age") int age) { int retval=eMper.hireEmp(id, name, age); if(retval==1) { return "successful"; }else { return "failed"; } } /** * 按ID查找一个雇员 * @param id * @return {"id":101,"name":"IOMCPELTEO","age":20} */ @RequestMapping(value="/findEmpById", method=RequestMethod.GET) public Emp findEmpById(@ModelAttribute(value="id") long id) { Emp emp=eMper.findById(id); return emp; } ... }
虽然又是个CRUD程序,但万丈高楼平地起。
END
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2017-09-25 【Canvas与艺术】绘制一款色彩斑斓的调色盘状时钟表盘
2017-09-25 【Canas与艺术】模拟手电照亮墙壁上的字,有点摸金校尉的感觉
2017-09-25 【Canvas与艺术】移动光源照亮墙上的字
2013-09-25 用Java发送HTML格式邮件测试类(支持中文)
2013-09-25 XAMPP安装、启动和使用
2013-09-25 【Canvas与诗词】风雨愁