现在有一个项目使用ssm框架,项目有一个表"zhongyao",属性有id,name,gongxiao,biaoji,有数据15条。前端页面类似于背单词的页面用于背中药,一块文本区域随机抽取zhongyao中的name和gongxiao,下面有两个按钮分别代表"掌握""不掌握",点击"掌握"就使zhongyao.biaoji="掌握",点击"不掌握"使zhongyao.biaoji="不掌握"。现在连接数据库,生产这个项目的代码
时间: 2023-07-19 15:38:52 浏览: 95
由于你没有提供具体的代码需求,我只能提供一些参考代码,希望能帮到你。
首先,需要在数据库中创建表"zhongyao",并插入15条数据:
```sql
CREATE TABLE zhongyao (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
gongxiao varchar(50) DEFAULT NULL,
biaoji varchar(10) DEFAULT NULL,
PRIMARY KEY (id)
);
INSERT INTO zhongyao (name, gongxiao, biaoji)
VALUES
('草药1', '功效1', NULL),
('草药2', '功效2', NULL),
('草药3', '功效3', NULL),
('草药4', '功效4', NULL),
('草药5', '功效5', NULL),
('草药6', '功效6', NULL),
('草药7', '功效7', NULL),
('草药8', '功效8', NULL),
('草药9', '功效9', NULL),
('草药10', '功效10', NULL),
('草药11', '功效11', NULL),
('草药12', '功效12', NULL),
('草药13', '功效13', NULL),
('草药14', '功效14', NULL),
('草药15', '功效15', NULL);
```
然后,需要在后端编写控制器和服务类来处理前端的请求和数据库操作。以下是示例代码:
控制器:
```java
@Controller
@RequestMapping("/zhongyao")
public class ZhongyaoController {
@Autowired
private ZhongyaoService zhongyaoService;
@GetMapping("/random")
@ResponseBody
public Map<String, Object> getRandomZhongyao() {
return zhongyaoService.getRandomZhongyao();
}
@PostMapping("/update/{id}")
@ResponseBody
public Map<String, Object> updateBiaoji(@PathVariable("id") int id, @RequestParam("biaoji") String biaoji) {
return zhongyaoService.updateBiaoji(id, biaoji);
}
}
```
服务类:
```java
@Service
public class ZhongyaoService {
@Autowired
private ZhongyaoMapper zhongyaoMapper;
public Map<String, Object> getRandomZhongyao() {
Map<String, Object> result = new HashMap<>();
Zhongyao zhongyao = zhongyaoMapper.getRandomZhongyao();
result.put("name", zhongyao.getName());
result.put("gongxiao", zhongyao.getGongxiao());
return result;
}
public Map<String, Object> updateBiaoji(int id, String biaoji) {
Map<String, Object> result = new HashMap<>();
int count = zhongyaoMapper.updateBiaoji(id, biaoji);
if (count > 0) {
result.put("success", true);
} else {
result.put("success", false);
result.put("msg", "更新失败");
}
return result;
}
}
```
Mapper接口和对应的XML文件:
```java
public interface ZhongyaoMapper {
@Select("SELECT * FROM zhongyao ORDER BY RAND() LIMIT 1")
Zhongyao getRandomZhongyao();
@Update("UPDATE zhongyao SET biaoji=#{biaoji} WHERE id=#{id}")
int updateBiaoji(@Param("id") int id, @Param("biaoji") String biaoji);
}
```
```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.mapper.ZhongyaoMapper">
<resultMap id="zhongyaoMap" type="com.example.entity.Zhongyao">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="gongxiao" column="gongxiao"/>
<result property="biaoji" column="biaoji"/>
</resultMap>
<select id="getRandomZhongyao" resultMap="zhongyaoMap">
SELECT * FROM zhongyao ORDER BY RAND() LIMIT 1
</select>
<update id="updateBiaoji">
UPDATE zhongyao SET biaoji=#{biaoji} WHERE id=#{id}
</update>
</mapper>
```
最后,在前端页面中使用jQuery和AJAX来实现随机抽取和更新标记的功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>背中药</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function() {
var zhongyaoId = 0;
function getRandomZhongyao() {
$.get("/zhongyao/random", function(data) {
$("#name").text(data.name);
$("#gongxiao").text(data.gongxiao);
zhongyaoId = data.id;
});
}
function updateBiaoji(biaoji) {
$.post("/zhongyao/update/" + zhongyaoId, {biaoji: biaoji}, function(data) {
if (data.success) {
getRandomZhongyao();
} else {
alert(data.msg);
}
});
}
$("#btn-mastery").click(function() {
updateBiaoji("掌握");
});
$("#btn-unmastery").click(function() {
updateBiaoji("不掌握");
});
getRandomZhongyao();
});
</script>
</head>
<body>
<p id="name"></p>
<p id="gongxiao"></p>
<button id="btn-mastery">掌握</button>
<button id="btn-unmastery">不掌握</button>
</body>
</html>
```
以上代码只是一个简单的示例,实际项目中还需要根据具体需求进行修改和完善。
阅读全文