来自 https://wenku.baidu.com/view/db50d32601020740be1e650e52ea551810a6c9cb.html
⾸先声明:只是⽤最简单的⽅法⼤致了解如何⽤存储过程开发,如果需要查看存储过程创建语法的⾃⾏百度搜索
⼀、⾸先创建最基本的数据库
CREATE TABLE `t_user` (
`id` varchar(255) NOT NULL,
`user_name` varchar(255) DEFAULT NULL,
`user_acct` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
按照版本不同进⾏适当修改,不要说什么sql不能⽤,这么简单的sql,⾃⼰⼿写⼀个适合的也很容易吧。
⼆、创建最简单的存储过程
CREATEPROCEDURE select_user(IN userId INT)
BEGIN
SELECT `name` FROM sys_user WHERE id = userId;
END
创建⼀个存储过程 IN 是输⼊参数可以理解为查询条件传参
三、知道如何在mysql中更改和调⽤存储过程
SET@userId='1';
CALL select_user(@userId);
设置传⼊参数
四、创建基本的SpringBoot项⽬
通过idea直接进⾏创建、很简单,简单的SpringMvc项⽬
加⼊mysql架包和yml或properties⽂件
POM⽂件
<!--mysql需要jar包 start-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mysql需要jar包 end-->
application.yml或application.properties(填写⾃⼰对应的数据库
五、编写controller,service,dao,mapper.xml⽂件
ProcedureController(注意:我的是post请求,如果不会⽤postman的童鞋,请更改为get
请求,注解为@GetMapper)
@RestController
@RequestMapping("procedure")
publicclass ProcedureController {
@Resource
private ProcedureService procedureService;
@PostMapping("/getParam")
public String getParam() {
Map<String,String> para = new HashMap<>(1);
para.put("userId","1");
String str = procedureService.getParam(para);
return str;
}
}
ProduceServiceImpl ⾯向接⼝编程(可以按照条件⾃⼰更改)debug调试的话可以知道str
中存放的具体数据是什么,
@Service
publicclass ProduceServiceImpl implements ProcedureService {
@Resource
private ProcedureDao procedureDao;
@Override
public String getParam(Map<String, String> param) {
Map<String , String> str = procedureDao.getParam(param);
return "123";
}
}
ProcedureDao:
publicinterface ProcedureDao {
Map<String , String> getParam(Map<String,String> param);
}
ProcedureDaoMapper.xml(记得⼀定要有返回类型):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.junittest.dao.ProcedureDao">
<parameterMap type="Map" id="getMap">
<parameter property="userId" jdbcType="VARCHAR" javaType="String" mode="IN"/>
</parameterMap>
<select id="getParam" parameterMap="getMap" statementType="CALLABLE" resultType="map">
{CALL select_user(?)}
</select>
</mapper>
六、运⾏调⽤接⼝即可