package com.attendance;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.attendance.mapper")
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class, args);
}
}
@MapperScan注解指明同一扫描路径
数据库配置
server:
port: 7070
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/attendance?useUnicode=true&useSSL=false&characterEncoding=UTF-8
MyBatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.attendance.entity
mapper-locations:指明MyBatis的xml文件所在位置
type-aliases-package: 指明实体类所在位置
在java文件路径和resources路径下新建mapper文件夹
**com.attendance.mapper.VocationMapper **
package com.attendance.mapper;
import com.attendance.entity.Vocation;
import org.springframework.stereotype.Repository;
@Repository
// @Mapper 可以使用@Mapper注解,但是每个类加注解较麻烦,所以统一配置@MapperScan在application启动类中
public interface VocationMapper {
void addVocation(Vocation vocation);
}
classpath:mapper/VocationMapper(resources下的mapper路径下)
<mapper namespace="com.attendance.mapper.VocationMapper">
<resultMap id="vocation" type="Vocation">
<id property="id" column="id"/>
<result property="applicant" column="applicant"/>
<result property="admin" column="admin"/>
<result property="date" column="date"/>
<result property="time" column="time"/>
<result property="leave_days" column="leave_days"/>
<result property="leave_date" column="leave_date"/>
<result property="leave_reason" column="leave_reason"/>
<result property="all_content" column="all_content"/>
<result property="read_state" column="read_state"/>
resultMap>
<insert id="addVocation" parameterType="vocation">
INSERT INTO
vocation(applicant,admin,date,time,leave_days,leave_date,leave_reason)
VALUES
(#{applicant},#{admin},#{date},#{time},#{leave_days},#{leave_date},#{leave_reason})
insert>
mapper>
package com.attendance.entity;
import lombok.Data;
@Data
public class Vocation {
private Integer id;
private String applicant;
private String admin;
private Date date;
private String time;
private String leave_days;
private String leave_date;
private String leave_reason;
private String all_content;
private String read_state;
}
package com.attendance.service;
import com.attendance.entity.Vocation;
public interface VocationService {
void addVocation(Vocation vocation);
}
VocationServiceImpl
package com.attendance.service.impl;
import com.attendance.entity.Vocation;
import com.attendance.mapper.VocationMapper;
import com.attendance.service.VocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
@Service
public class VocationServiceImpl implements VocationService {
@Autowired // 自动装配
private VocationMapper vocationMapper;
@Override
public void addVocation(Vocation vocation) {
vocationMapper.addVocation(vocation);
}
}
mybatis:
type-aliases-package: com.attendance.entity
package com.attendance.mapper;
import com.attendance.entity.Staff;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
@Repository
public interface StaffRoleMapper {
@Insert("INSERT INTO staff(staff_name,staff_date) " +
"VALUES (#{staff.staffName},#{staff.staffDate})")
@Options(useGeneratedKeys = true, keyProperty = "staff.id")
void addStaff(@Param("staff") Staff staff);
@Select("SELECT id FROM role WHERE role = #{role} ")
int getRoleId(String role);
@Insert("INSERT INTO staff_roles(staff_id,roles_id) VALUES (#{staff_id},#{role_id})")
void addStaffWithRole(@Param("staff_id") int staffId, @Param("role_id") int roleId);
@Delete("DELETE FROM staff_roles WHERE staff_id = #{staff_id}")
void delStaffWithRole(@Param("staff_id") int staffId);
@Delete("DELETE FROM staff WHERE id = #{staff_id}")
void delStaff(@Param("staff_id") int staffId);
}
Service服务类代码与配置文件形式一样