SSM+Mysql+layui 新闻网站+增删改查后台代码(2)

这篇博客介绍了如何使用SSM(Spring、SpringMVC、MyBatis)和MySQL数据库结合Layui前端框架,实现新闻网站的增删改查后台代码。内容涵盖Controller和Service层的实现。
摘要由CSDN通过智能技术生成

文章的增删改查

controller

// 管理模块中,总的文章查询(不包括被删除的)
	@RequestMapping("/articleSelect1")
	@ResponseBody
	private Map<String, Object> selectAll1(int limit, int page, String wenzhang, String leixing, String bu,
			Integer lan) {
   
		Map<String, Object> map = new HashMap<String, Object>();
//方法中的wenzhang,leixing,bu是查找条件参数
		PageHelper.startPage(page, limit);//maven引入pagehelper分页
		List<Article> list = articleService.getAll(wenzhang, leixing, bu, lan);
		long count = articleService.getAllCount(wenzhang, leixing, bu, lan);

		map.put("data", list);
		map.put("code", 0);
		map.put("count", count);
		//layui前台接收的数据格式如上
		return map;

	}
//添加一篇文章
	@RequestMapping("/articleAdd1")
	@ResponseBody
	private Map<String, Object> add1(@RequestBody HashMap<String, String> map2) throws Exception {
   
		Map<String, Object> map = new HashMap<String, Object>();
		int caId = Integer.parseInt(map2.get("caId"));
		String arTitle = map2.get("arTitle");
		String arContent = map2.get("arContent");
		String arUser = map2.get("arUser");
		String arState = map2.get("arState");
		String arDuty = map2.get("arDuty");
		String arSpare1 = map2.get("arSpare1");
		String arSpare2 = map2.get("arSpare2");
		String arSpare3 = map2.get("arSpare3");
		String arPosition = map2.get("arPosition");
		String arImage = map2.get("arImage");
		String arDetail = map2.get("arDetail");

		Article article = new Article();
		long maxcount = articleService.getAllCount();
		long max = maxcount + 1;
		/* System.out.println(max); */
		String arNumber = String.valueOf(max);
		article.setArNumber(arNumber);
		//获取文章总量,为文章赋值序列号,便于后期排序

		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
		String da = format.format(date);
		String da2 = format2.format(date);
		double random = Math.random() * 10000;
		String arId = da + (int) random;
		String arTime = da2;
		//把日期转化为字符格式,用时间字符串加随机数做文章Id
		System.out.println(arSpare1 + arState);

		article.setArState(arState);
		article.setArI
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架和uni-app可以很好地结合使用来实现增删改查功能。下面是一个示例: 1. 在SSM框架中,使用MyBatis进行数据库操作。首先,创建一个Mapper接口,定义增删改查的方法。例如,创建一个UserMapper接口: ```java public interface UserMapper { void insert(User user); void delete(int id); void update(User user); User select(int id); } ``` 2. 在MyBatis的配置文件中,配置Mapper接口的映射关系。例如,创建一个userMapper.xml文件: ```xml <mapper namespace="com.example.mapper.UserMapper"> <insert id="insert" parameterType="com.example.model.User"> INSERT INTO user (id, name, age) VALUES (#{id}, #{name}, #{age}) </insert> <delete id="delete" parameterType="int"> DELETE FROM user WHERE id = #{id} </delete> <update id="update" parameterType="com.example.model.User"> UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id} </update> <select id="select" parameterType="int" resultType="com.example.model.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 3. 在Spring MVC中,创建一个Controller,处理前端请求并调用对应的Mapper方法。例如,创建一个UserController: ```java @Controller @RequestMapping("/user") public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/add") @ResponseBody public String addUser(User user) { userMapper.insert(user); return "添加成功"; } @RequestMapping("/delete") @ResponseBody public String deleteUser(int id) { userMapper.delete(id); return "删除成功"; } @RequestMapping("/update") @ResponseBody public String updateUser(User user) { userMapper.update(user); return "更新成功"; } @RequestMapping("/get") @ResponseBody public User getUser(int id) { return userMapper.select(id); } } ``` 4. 在uni-app中,使用Vue.js进行前端开发。可以通过发送HTTP请求来调用SSM框架中的接口。例如,使用uni.request方法发送请求: ```javascript // 添加用户 uni.request({ url: 'http://localhost:8080/user/add', method: 'POST', data: { id: 1, name: '张三', age: 20 }, success: function(res) { console.log(res.data); } }); // 删除用户 uni.request({ url: 'http://localhost:8080/user/delete', method: 'POST', data: { id: 1 }, success: function(res) { console.log(res.data); } }); // 更新用户 uni.request({ url: 'http://localhost:8080/user/update', method: 'POST', data: { id: 1, name: '李四', age: 25 }, success: function(res) { console.log(res.data); } }); // 获取用户 uni.request({ url: 'http://localhost:8080/user/get', method: 'GET', data: { id: 1 }, success: function(res) { console.log(res.data); } }); ``` 这样,就可以通过uni-app发送HTTP请求来实现SSM框架中的增删改查功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值