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