SpringBoot(代码实现)

按照我的博客SpringBoot进行配置,也可以进行实现下面的代码!

POJO类:(自己生成get/set方法)

public class User implements Serializable{
	
	private static final long serialVersionUID = -5846970261372764303L;

	private Long id;

    // 用户名
    private String userName;

    // 密码
    private String password;

	// 姓名
    private String name;

    // 年龄
    private Integer age;

    // 性别,1男性,2女性
    private Integer sex;

    // 出生日期
    private Date birthday;

    // 创建时间
    private Date created;

    // 更新时间
    private Date updated;
    
    @Override
    public String toString() {
        return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" + name
                + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created
                + ", updated=" + updated + "]";
    }
}

Controller层:

@RestController
public class UserController {
	
	//注入service
	@Autowired
	private UserService userService;
	/*
	 * 查询所有
	 * @GetMapping 里面包含了 @RequestMapping + method=RequestMethod.GET)
	 */
	@GetMapping("/queryall")
	//@RequestMapping(value="/queryall" ,method=RequestMethod.GET)
	public List<User> queryUserAll(){
		return this.userService.queryUserAll();
	}
	/*
	 * 根据id进行查询
	 */
	@GetMapping("/queryId")
	public User queryById(Long id ) {
		return this.userService.queryById(id);
	}
}

Service层:

@Service
public class UserService {
	
	@Autowired
	private UserMapper userMapper;

	public User queryById(Long id){
		return this.userMapper.queryById(id);
	}

	public List<User> queryUserAll() {
		return this.userMapper.ueryUserAll();
	}
}

Mapper接口:

@Mapper
public interface UserMapper {

	User queryById(Long id);

	public List<User> ueryUserAll();

	public int add(User user);

	public int update(@Param("id") Integer id, @Param("user") User user);

	public int delete(Integer id);
}

SqlMapper.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="cn.yanqi.mapper.UserMapper">

	<!-- 根据id进行查询 -->
    <select id="queryById" resultType="User">
       select * from tb_user where id = #{id}
</select>

	<!-- 查询所有 -->
	<select id="ueryUserAll" resultType="User">
		select * from tb_user
	</select>

</mapper>

jsp页面

<script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
	<script type="text/javascript">
			$.ajax({
			   type: "get",
			   url: "http://localhost:8080/queryall",
			   success: function(data){
				   var t = "";
				   $(data).each(function(i){
					   t += "<tr><td>" + data[i].id 
					      + "</td><td>"+ data[i].name
					      + "</td><td>"+ data[i].age+"</td></tr>"
				   });
				   $("table").append(t);
			   }
			});
	</script>
	<table  align="center" border="1" cellpadding="8px" cellspacing="0px" width="70%">
		<tr>
			<td>id</td>
			<td>name</td>
			<td>age</td>
		</tr>
	</table>

测试:

在这里插入图片描述

日志:

Spring Boot对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置

#设置日志级别
#print sql
logging.level.cn.yanqi=DEBUG
#print logging
logging.level.org.springframework=DEBUG

猜你喜欢

转载自blog.csdn.net/Denial_learn/article/details/103471657