创建数据库mybatis,并在其中创建表customer和student。
customer表:
创建SpringBoot工程。
创建完工程后更改为本地的maven。
更改完后导入依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hxic</groupId>
<artifactId>hb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在application.properties添加数据库连接和mapper扫描。
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=x5
mybatis.mapper-locations=classpath:mapper/*.xml
spring.web.resources.static-locations= classpath:/templates/,classpath:/templates/static/
创建Student和Customer实体类(一定要和数据库字段名一致)。
Student类:
package com.hxic.hb.pojo;
public class Student {
private Integer id;
private String name;
private String sno;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
}
Customer类:
package com.hxic.hb.pojo;
public class Customer {
private Integer id;
private String sname; // s_name,sName,s
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
创建CustomerDao和StudentDao层。
CustomerDao:
package com.hxic.hb.dao;
import com.hxic.hb.pojo.Customer;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface CustomerDao {
// dao ---> mapper.xml
public List<Customer> query(@Param("customer") Customer customer, @Param("pageNo") Integer pageNo);
public void add(Customer customer);
}
StudentDao:
package com.hxic.hb.dao;
import com.hxic.hb.pojo.Student;
public interface StudentDao {
public void add(Student student);
}
在resources下创建mapper文件夹并在下面创建studentMapper.xml和customerMapper.xml文件添加sql语句(这里加入了if判断可以根据姓名或者职业查询实现条件查询)。
studentMapper.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">
<!-- 1.namespace:接口的一个路径 ,2.id :接口下抽象方法名
3.接口的返回值和resultType 对应上 ,4接口的参数 对应上-->
<mapper namespace="com.hxic.hb.dao.StudentDao">
<insert id="add" >
insert into student (id,name ) values (#{id},#{name})
</insert>
</mapper>
customerMapper.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">
<!-- 1.namespace:接口的一个路径 ,2.id :接口下抽象方法名
3.接口的返回值和resultType 对应上 ,4接口的参数 对应上-->
<mapper namespace="com.hxic.hb.dao.CustomerDao" >
<select id="query" resultType="com.hxic.hb.pojo.Customer" >
select * from customer
<where>
<!--接口参数名-->
<if test="customer.sname != null and customer.sname != ''">
and sname=#{customer.sname}
</if>
<if test="customer.jobs != null and customer.jobs != ''">
and jobs =#{customer.jobs}
</if>
<if test="pageNo !=null and pageNo !=''">
limit 0,#{pageNo}
</if>
</where>
</select>
<insert id="add" >
insert into customer (username,jobs,phone) values (#{sname},#{jobs},#{phone})
</insert>
</mapper>
创建service层(这里把student和customer同时使用一个添加)。
package com.hxic.hb.service;
import com.hxic.hb.pojo.Customer;
import com.hxic.hb.pojo.Student;
import java.util.List;
public interface CustomerService {
public List<Customer> query(Customer customer, Integer pageNo);
public void add(Customer customer, Student student);
}
创建service的接口实现类(CustomerServiceImpl)(这里的@Transactional是spring提供的事务管理如果不使用这个数据就会出现添加错误)(这里调用的两个dao可能会出现红线但不会报错)。
package com.hxic.hb.service.impl;
import com.hxic.hb.dao.CustomerDao;
import com.hxic.hb.dao.StudentDao;
import com.hxic.hb.pojo.Customer;
import com.hxic.hb.pojo.Student;
import com.hxic.hb.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerDao dao;
@Autowired
StudentDao studentDao ;
public List<Customer> query(Customer ci,Integer pageNo){
//List<Customer> list = dao.query();
return dao.query(ci,pageNo);
}
@Transactional
public void add(Customer customer, Student student) {
dao.add(customer);
System.out.println(1/0); //模拟异常
studentDao.add(student);
}
}
创建Controller层(@RestController是让controller不进入视图解析器返回字符串可以不使用@RestController要想让他不返回字符串就使用@Controller如果不想改动@Controller 可以在想要返回字符串的方法上加@ResponseBody)。
package com.hxic.hb.contoller;
import com.hxic.hb.pojo.Customer;
import com.hxic.hb.pojo.Student;
import com.hxic.hb.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CustomerController {
//CustomerService service = new CustomerServiceImpl();
@Autowired
CustomerService service;
// 传递一个参数 参数名称 和 pojo类的属性名 和 数据库的列名一致
//1 如果接收的参数名是 pojo的属性名,那么, dao层接口 直接有#{}或者${} 传递到mapper.xml
//2 如果接收的参数名不在pojo的属性中 那么通过@Param("customer") 将值传递到mapper.xml 中
@RequestMapping("query")
public List<Customer> query(Customer customer,Integer pageNo){ // spring boot controller方法返回值看可以自动转json,ssm框架不行
List<Customer> a=service.query(customer,pageNo);
return a;
}
@RequestMapping("add")
public String add(Customer customer, Student student){
service.add(customer,student);
return null;
}
}
在SpringBoot自带的启动类上加@MapperScan("xxxx.xxxx.xxx")扫描mapper接口(注意:SpringbootMybatisApplication是在创建项目时自动生成的他会创建在你自定义的包下)。
package com.hxic.hb;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
@MapperScan({"com.hxic.**.dao"})
public class HbApplication {
//spring boot == spring + spring mvc
public static void main(String[] args) {
SpringApplication.run(HbApplication.class, args);
}
}
点击启动后在浏览器中输入网址:http://localhost:8080/query
就可以输出你数据库中的数据
想要添加数据就把路径后的query改为add并在后面拼接添加的值即可
————————————————
版权声明:本文为CSDN博主「执笔撰写天下事」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_59078057/article/details/123755125