上一篇:Spring Boot 2.0.2 教程 - 整合JdbcTemplate - 06
pom.xml添加相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> <version>1.5.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- 单元测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 引入jdbc和mysql驱动包,spring boot 默认会使用HikariCP作为数据库连接池 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!-- spring-boot整合mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> </dependencies> <build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
项目整体结构
application.properties新增mysql和mybatis相关配置
#mysql spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/app_test?Unicode=true&characterEncoding=UTF-8&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=root #mybatis mybatis.mapper-locations=classpath:mapper/*Dao.xml mybatis.config-location=classpath:mapper/conf/mybatis_cfg.xml mybatis.type-aliases-package=com.example.demo.model
根据项目结构新增相关包
model,dao以及mapper dao配置文件为自动生成,可参考文章 idea配置自动生成mybaits实体类和mapper.xml
dao接口需要添加注解:@mapper
参考如下:
Example.java public class Example implements Serializable { private Integer id; private String userName; private String addr; private Byte sex; //省略get set toString方法 }
ExampleDao.java @Mapper public interface ExampleDao { int deleteByPrimaryKey(Integer id); int insert(Example record); int insertSelective(Example record); Example selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Example record); int updateByPrimaryKey(Example record); }
ExampleDao.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.example.demo.dao.ExampleDao"> <resultMap id="BaseResultMap" type="com.example.demo.model.Example"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="user_name" jdbcType="VARCHAR" property="userName" /> <result column="addr" jdbcType="VARCHAR" property="addr" /> <result column="sex" jdbcType="TINYINT" property="sex" /> </resultMap> <sql id="Base_Column_List"> id, user_name, addr, sex </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from example where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from example where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.example.demo.model.Example" useGeneratedKeys="true"> insert into example (user_name, addr, sex ) values (#{userName,jdbcType=VARCHAR}, #{addr,jdbcType=VARCHAR}, #{sex,jdbcType=TINYINT} ) </insert> <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.example.demo.model.Example" useGeneratedKeys="true"> insert into example <trim prefix="(" suffix=")" suffixOverrides=","> <if test="userName != null"> user_name, </if> <if test="addr != null"> addr, </if> <if test="sex != null"> sex, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="userName != null"> #{userName,jdbcType=VARCHAR}, </if> <if test="addr != null"> #{addr,jdbcType=VARCHAR}, </if> <if test="sex != null"> #{sex,jdbcType=TINYINT}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.example.demo.model.Example"> update example <set> <if test="userName != null"> user_name = #{userName,jdbcType=VARCHAR}, </if> <if test="addr != null"> addr = #{addr,jdbcType=VARCHAR}, </if> <if test="sex != null"> sex = #{sex,jdbcType=TINYINT}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.example.demo.model.Example"> update example set user_name = #{userName,jdbcType=VARCHAR}, addr = #{addr,jdbcType=VARCHAR}, sex = #{sex,jdbcType=TINYINT} where id = #{id,jdbcType=INTEGER} </update> </mapper>
mybatis_cfg.xml,为mybaits配置文件
参考如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="cacheEnabled" value="true" /><!-- 是否开启缓存 --> <setting name="lazyLoadingEnabled" value="true" /> <setting name="multipleResultSetsEnabled" value="true" /> <setting name="useColumnLabel" value="true" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> </configuration>
dao测试类
ExampleDaoTest.java @RunWith(SpringRunner.class) @SpringBootTest(classes = DemoApplication.class) //DemoApplication.class 为项目启动的那个类 public class ExampleDaoTest { @Autowired private ExampleDao exampleDao; @Test public void saveBean(){ Example bean = new Example(); bean.setUserName("ZhangSan"); bean.setAddr("ShangHai"); bean.setSex((byte)1); exampleDao.insert(bean); } @Test public void getBean(){ Example bean = exampleDao.selectByPrimaryKey(3360); System.out.println(bean); } }