springboot+lombok+thymeleaf+mybais简单的写一个增删改查方法,前端是bootstrap框架
此文章只作为记录自己的学习,关于命名规则问题我随心,仅供参考
1.创建项目选中Web、Thymeleaf、MySql依赖,然后额外导入lombok、数据源Druid、以及通用mapper依赖,
<!--阿里druid数据源启动器-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<!--通用mapper常见启动器-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2.在src/main/resources下创建application.yml文件,配置mybatis
```xmlspring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/durgwh?serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其它配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
thymeleaf:
cache: false
#MyBatis相关配置
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.bt.entity
#日志级别
logging:
level:
com.bt: debug
3.我们可以通过自定义配置类来扩展Spring boot的springmvc功能,我们只需要写一个配置类实现WebMvcConfigurer即可,我们创建com.bt.config包并且创建类MvcConfig,示例如下:目的直接去某个页面时不用去controller里去写请求
@Configuration
public class MvcConfig implements WebMvcConfigurer {
//扩展视图控制器映射器
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/welcome请求 到达welcome页面
registry.addViewController("/welcome").setViewName("login");
registry.addViewController("/main").setViewName("main");
registry.addViewController("/druglist").setViewName("druglist");
registry.addViewController("/addinslist").setViewName("/addinslist");
}
4.创建包com.bt.entity,并创建Instrument实体类,注意导包别导错。
package com.bt.entity;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Data
@Table(name="instrument")
public class Instrument {
@Id
@KeySql(useGeneratedKeys = true)
private Integer id;
private String name;
private String type;
private Integer number;
@DateTimeFormat(pattern =" yyyy-mm-dd")
private Date datemanufacture;
private String probatch;
@DateTimeFormat(pattern =" yyyy-mm-dd")
private Date warehousdate;
private String position;
private String pic;
}
5.编写com.bt.dao.mapper接口(注意mapper的注解,以及导包问题,一定要到tk下的mybatis的Mapper)
package com.bt.dao;
import com.bt.entity.Instrument;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
@org.apache.ibatis.annotations.Mapper
public interface InstrumentMapper extends Mapper<Instrument>{
List<Instrument> getListByCon(Instrument instrument);
}
6.编写模糊查sql的xml文件(注意:namespace命名空间是dao层的映射mapper地址,注意id必须和dao层的方法相同)
<mapper namespace="com.bt.dao.InstrumentMapper">
<!--查询所有 -->
<select id="getListByCon" resultType="instrument" parameterType="instrument">
select * from instrument
<trim prefix="where" prefixOverrides="and || or">
<if test="name!=null and name!=''">and name like concat('%',#{name},'%')</if>
<if test="position!=null and position!=''">and position=#{position}</if>
</trim>
</select>
</mapper>
7.Service接口
//模糊查
List<Instrument> getListByCon(Instrument instrument);
//查看详情
Instrument selectByIns(Integer id);
//修改
int updateByIns(Instrument instrument);
//增加
int addIns(Instrument instrument);
//删除
int delIns(Integer id);
8.Service的实现类
@Service
public class InstrumentServiceImpl implements InstrumentService {
@Autowired
private InstrumentMapper mapper;
@Override
public List<Instrument> getListByCon(Instrument instrument) {
return mapper.getListByCon(instrument);
}
@Override
public Instrument selectByIns(Integer id) {
return mapper.selectByPrimaryKey(id);
}
@Override
public int updateByIns(Instrument instrument) {
return mapper.updateByPrimaryKeySelective(instrument);
}
@Override
public int addIns(Instrument instrument) {
return mapper.insertSelective(instrument);
}
@