前言
这篇文章主要介绍的是对于数据库的增删查改以及稍微难一点的联级查询!废话不多说,我们开始吧!
加载依赖
<!--JDBC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jx?useUnicode=true & characterEncoding=utf-8 &
useSSL=true & serverTimezone=Asia/Shanghai
username: root
password: 196811
mybatis:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.example.jx.bean
建表
添加数据
我们新建两个实体类
package com.example.jx.bean;
public class User {
private int id;
private String name;
private Iitem iitem;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Iitem getIitem() {
return iitem;
}
public void setIitem(Iitem iitem) {
this.iitem = iitem;
}
}
package com.example.jx.bean;
public class Iitem {
private int id;
private int user_id;
private String interest;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
}
接口
package com.example.jx.Dao;
import com.example.jx.bean.User;
import org.springframework.stereotype.Repository;
@Repository
public interface TestDao {
public void add(User user);
}
事务
package com.example.jx.server;
import com.example.jx.Dao.TestDao;
import com.example.jx.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("TestServer")
public class TestServer {
@Autowired
TestDao testDao;
public void add(User user){
testDao.add(user);
}
}
mapper
<?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.jx.Dao.TestDao">
<insert id="add">
INSERT INTO user VALUES (null, #{name})
</insert>
</mapper>
控制端
package com.example.jx.Controller;
import com.example.jx.bean.User;
import com.example.jx.server.TestServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Hello {
@Autowired
TestServer testServer;
@RequestMapping("/{name}")
@ResponseBody
public String hello(@PathVariable("name") String name){
User user = new User();
user.setName(name);
testServer.add(user);
return "Hello World";
}
}
效果图
删除数据
代码如下
public void delete(@Param("id") int id);
<delete id="delete">
delete from user where user.id=#{id}
</delete>
public void delete(int id){
testDao.delete(id);
}
package com.example.jx.Controller;
import com.example.jx.bean.User;
import com.example.jx.server.TestServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Hello {
@Autowired
TestServer testServer;
@RequestMapping("/{id}")
@ResponseBody
public String hello(@PathVariable("id") int id){
testServer.delete(id);
return "Hello World";
}
}
效果
修改
代码如下
public void edit(@Param("id") int id,
@Param("name") String name);
<update id="edit">
update user set name=#{name} where id=#{id}
</update>
public void edit(int id, String name){
testDao.edit(id, name);
}
效果
好了现在我们来查询了,我特意把查询单独拿出来。看好了,它来了!
查询
简单查询
public User query(@Param("id") int id);
<select id="query" resultType="User">
select * from user where id=#{id}
</select>
public User query(int id){
return testDao.query(id);
}
@Controller
public class Hello {
@Autowired
TestServer testServer;
@RequestMapping("/{id}")
@ResponseBody
public User hello(@PathVariable("id") int id){
return testServer.query(id);
}
}
效果
一对一查询
我们继续来点稍微复杂查询–》多表联合查询–》
我这里给出两种方式
- 复杂一点的
代码如下
public User query2(@Param("id") int id);
<select id="query2" resultMap="userResult">
select user.id as id,
user.name as name,
iitem.id as iitem_id,
iitem.user_id as user_id,
iitem.interest as interest from user join iitem on user.id=user_id where user.id=#{id}
</select>
<resultMap id="userResult" type="User">
<id property="id" column="user_id" />
<result property="name" column="name" />
<association property="iitem" column="iitem" javaType="Iitem" resultMap="iitemResult" />
</resultMap>
<resultMap id="iitemResult" type="Iitem">
<id property="id" column="id" />
<result property="user_id" column="user_id" />
<result property="interest" column="interest" />
</resultMap>
public User query2(int id){
return testDao.query2(id);
}
- 简洁一些的
就xml有点不一样,其他的都一样的,就是合在了一起写!
<resultMap id="userResult" type="User">
<id property="id" column="user_id" />
<result property="name" column="name" />
<association property="iitem" javaType="Iitem">
<id property="id" column="iitem_id"/>
<result property="user_id" column="user_id"/>
<result property="interest" column="interest" />
</association>
</resultMap>
一对多查询
同样的我们要修改两个地方:
实体类:
package com.example.jx.bean;
import java.util.List;
public class User {
private int id;
private String name;
private List<Iitem> iitem;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Iitem> getIitem() {
return iitem;
}
public void setIitem(List<Iitem> iitem) {
this.iitem = iitem;
}
}
xml文件
<resultMap id="userResult" type="User">
<id property="id" column="user_id" />
<result property="name" column="name" />
<collection property="iitem" javaType="java.util.List" ofType="Iitem">
<id property="id" column="iitem_id"/>
<result property="user_id" column="user_id"/>
<result property="interest" column="interest" />
</collection>
</resultMap>
结果
到此,架设网站的基本东西----封装数据!学完到这一步,你就基本可以做一些比较简单的项目了,比如博客网站,物流管理网站,等等基本都会怎么封装数据了。好好学习吧,少年!不熬夜,你怎么能成功,自己的谎言如果把自己骗进去了而且还深信不疑的话…真可怜,孩子。。。。。。