SpringBoot对JavaWeb项目的简单部署
1.基础依赖导入(pom文件)
<!-- MyBatis整合Spring-boot-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- mysql连接驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- Spring-boot-web项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Druid数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
2.文件配置信息(yml文件)
#tomcat端口号
server:
port: 8081
#连接数据库信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///air?serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
#spring boot已经集成logback日志
logging:
level:
com.hk.firstspringboot.mapper: DEBUG #扫描这个包下,(可以扫描mapper包下的接口类,打印出Sql语句)
# mybatis配置
mybatis:
# 扫描映射文件
mapper-locations: classpath:mapper/*.xml
# 配置别名扫描的包
#type-aliases-package: com.hk.firstspringboot.entity
#configuration:
# 开启驼峰映射配置
#map-underscore-to-camel-case: true
3.其它注意:
1.mapper 接口包要扫描:在启动类上加注解:@MapperScan(basePackages = "例子:com.hk.easycodetest.mapper")
4.SpringBoot开启使用事务
启动类注解:
@EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@Transactional(propagation=Propagation.REQUIRED) //默认等同于@Transactional
表示当前方法必须运行在事务中。如果当前事务存在,方法将会在该事务中运行。否则,会启动一个新的事务
5.mapping的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">
<mapper namespace="com.hk.easycodetest.dao.DistrictDao">
<resultMap type="com.hk.easycodetest.entity.District" id="DistrictMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="DistrictMap">
select
id, name
from air.district
where id = #{id}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="DistrictMap">
select
id, name
from air.district
limit #{offset}, #{limit}
</select>
<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="DistrictMap">
select
id, name
from air.district
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="name != null and name != ''">
and name = #{name}
</if>
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into air.district(name)
values (#{name})
</insert>
<!--通过主键修改数据-->
<update id="update">
update air.district
<set>
<if test="name != null and name != ''">
name = #{name},
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete from air.district where id = #{id}
</delete>
</mapper>