Gradle + Springboot + SSM整合

1.配置文件application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm_springboot?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root

2.controller类

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ssm.ssm_springboot.dao.UserDao;
import com.ssm.ssm_springboot.domain.User;

@Controller
public class UserController {

	@Autowired
	private UserDao userdao;

	// 查询所有用户
	@RequestMapping("/selectAll")
	public String selectAll() {
		List<User> selectAll = userdao.selectAll();
		for (User user : selectAll) {
			System.out.println(user);
		}
		return "index";
	}

	// 根据姓名查询
	@RequestMapping("/selectUserName")
	public String selectUserName(String username) {
		User user = userdao.selectUserName(username);
		System.out.println(user);
		return "index";
	}

	// 保存数据
	@RequestMapping("/insertUser")
	public String insertUser(User user) {
		System.out.println(user);
		int count = userdao.insertUser(user);
		System.out.println(count);
		return "index";
	}

	// 根据ID修改用户名
	@RequestMapping("updateUser")
	public String updateUser(User user) {
		int count = userdao.updateUser(user);
		System.out.println(count);
		return "index";
	}

	// 根据姓名删除用户
	@RequestMapping("deleteUser")
	public String deleteUser(User user) {
		int count = userdao.deleteUser(user);
		System.out.println(count);
		return "index";
	}

//使用SqlProvider查询 可以直接用程序判断逻辑

	// 查询所有用户
	@RequestMapping("/selectAll2")
	public String selectAll2() {
		List<User> selectAll = userdao.selectAll2();
		for (User user : selectAll) {
			System.out.println(user);
		}
		return "index";
	}

	// 根据姓名查询使用SqlProvider来查询
	@RequestMapping("/selectUserName2")
	public String selectUserName2(String username) {
		User user = userdao.selectUserName2(username);
		System.out.println(user);
		return "index";
	}

	// 保存数据
	@RequestMapping("/insertUser2")
	public String insertUser2(User user) {
		System.out.println(user);
		int count = userdao.insertUser2(user);
		System.out.println(count);
		return "index";
	}

	// 根据ID修改用户名
	@RequestMapping("updateUser2")
	public String updateUser2(User user) {
		int count = userdao.updateUser2(user);
		System.out.println(count);
		return "index";
	}

	// 根据姓名删除用户
	@RequestMapping("deleteUser2")
	public String deleteUser2(User user) {
		int count = userdao.deleteUser2(user);
		System.out.println(count);
		return "index";
	}

}

3.dao类

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import com.ssm.ssm_springboot.domain.User;
import com.ssm.ssm_springboot.provider.UserProvider;

@Mapper
public interface UserDao {

	@Select("select * from user")
	List<User> selectAll();

	@Select("select * from user where username = #{username}")
	User selectUserName(String username);

	@Insert("insert into user (username,password) values(#{username},#{password})")
	int insertUser(User user);

	@Update("update user set username=#{username} where id=#{id}")
	int updateUser(User user);

	@Delete("delete from user where username=#{username}")
	int deleteUser(User user);
//@SelectProvider type 指定需要跳转的类,method 指定需要跳转的方法
	@SelectProvider(type = UserProvider.class, method = "selectAll2")
	List<User> selectAll2();

	@SelectProvider(type = UserProvider.class, method = "selectUserName2")
	User selectUserName2(String username);

	@InsertProvider(type = UserProvider.class, method = "insertUser2")
	int insertUser2(User user);

	@UpdateProvider(type = UserProvider.class, method = "updateUser2")
	int updateUser2(User user);

	@DeleteProvider(type = UserProvider.class, method = "deleteUser2")
	int deleteUser2(User user);
}
4.SQLProvider类

import org.apache.ibatis.jdbc.SQL;
import com.ssm.ssm_springboot.domain.User;
public class UserProvider {

	// 查询所有用户
	public String selectAll2() {

		return new SQL() {
			{
				SELECT("*");
				FROM("user");
			}
		}.toString();
	}

	// 根据用户名查询用户
	public String selectUserName2(String usernmae) {
		return new SQL() {
			{
				SELECT("*");
				FROM("user");
				WHERE("username=#{username}");
			}
		}.toString();
	}

	// 插入数据
	public String insertUser2(User user) {
		return new SQL() {
			{
				INSERT_INTO("user");
				VALUES("username", "#{username}");
				VALUES("password", "#{password}");
			}
		}.toString();
	}

	// 更新数据
	public String updateUser2(User user) {
		return new SQL() {
			{
				UPDATE("user");
				SET("username=#{username}");
			}
		}.toString();
	}

	// 删除数据
	public String deleteUser2(User user) {
		return new SQL() {
			{
				DELETE_FROM("user");
				WHERE("id=#{id}");
			}
		}.toString();
	}
}

5.user实体

public class User {

	private Integer id;
	private String username;
	private String password;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值