源码
链接:https://pan.baidu.com/s/1b_PX-WyDm7W_JVafZvFCWQ
提取码:k6hx
实验任务1:建库建表
在mysql中创建mybatis数据库,并创建customer表,设置id,主键自增,添加sname,jobs,phone字段,如图。
实验任务2:创建项目,引入相关依赖
实验任务3:创建application.properties配置
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 实验任务4:创建customer的pojo类
package com.hxci.jz.pojo;
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
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 getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", username='" + username + '\'' +
", jobs='" + jobs + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
实验任务5:创建customerDao接口
通过@Param注解传值
package com.hxci.jz.dao;
import com.hxci.jz.pojo.Customer;
import com.hxci.jz.pojo.Student;
import java.util.List;
public interface CustomerDao {
public List<Customer> query();
public void add(Customer customer);
}
实验任务6:创建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.hxci.jz.dao.CustomerDao">
<select id="query" resultType="com.hxci.jz.pojo.Customer">
select * from customer
</select>
<insert id="add">
insert into customer (username,jobs,phone) value (#{username},#{jobs},#{phone})
</insert>
<!-- <delete id="update">
update customer set username=#{username},jobs=#{jobs},phone=#{phone} where id=#{id}
</delete>
<delete id="delete">
delete from customer where id=#{id}
</delete>
<select id="queryById" resultType="com.hxci.jz.pojo.Customer">
select * from customer where id=#{id}
</select>-->
</mapper>
实验任务7:创建CustomerService和CustomerServiceImpl
CustomerService
package com.hxci.jz.service;
import com.hxci.jz.pojo.Customer;
import com.hxci.jz.pojo.Student;
import java.util.List;
public interface CustomerService {
public List<Customer> query();
void add(Customer customer, Student student);
}
CustomerServiceImpl
package com.hxci.jz.service;
import com.hxci.jz.dao.CustomerDao;
import com.hxci.jz.dao.StudentDao;
import com.hxci.jz.pojo.Customer;
import com.hxci.jz.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService{
@Autowired
CustomerDao dao;
@Autowired
StudentDao studentDao;
public List<Customer> query(){
return dao.query();
}
@Override
public void add(Customer customer, Student student) {
dao.add(customer);
System.out.println(1/0);//模拟异常
studentDao.add(student);
}
}
实验任务8:创建CustomerController
package com.hxci.jz.controller;
import com.hxci.jz.pojo.Customer;
import com.hxci.jz.pojo.Student;
import com.hxci.jz.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CustomerController {
//CustomerService service = new CustomerServiceImpl();
@Autowired
CustomerService service;
@RequestMapping("query")
public List<Customer> query(){ // spring boot controller方法返回值看可以自动转json,ssm框架不行
List<Customer> list=service.query();
return list;
}
@RequestMapping("add")
public String add(Customer customer, Student student){
service.add(customer,student);
return null;
}
}