标签:启动 .config rom .data you sel dep mysq model
mybatis-spring-boot-starter主要有两种解决方案,一种是使用注解解决一切问题,一种是简化后的老传统。无论哪种,第一步是必不可少的。
无配置文件注解版
第一步:添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
第二步:在application.properties中添加MySql属性
spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = spring.datasource.username = spring.datasource.password =
注意:springboot会自动加载spring.datasource.*相关配置,数据源就会自动注入到sqlSessionFactory中,sqlSessionFactory会自动注入到Mapper中。
在启动类中添加对mapper包扫描@MapperScan,或者直接在Mapper类上面添加注解@Mapper。
第三步:开发Mapper
public interface TurbineDao { @Select("SELECT * FROM m_turbine WHERE turbine_code = #{turbineCode}") @Results({ @Result(property = "turbineCode", column = "turbine_code"), @Result(property = "deviceCode", column = "device_code") }) public List<Turbine> findOne(String turbineCode); }
注意1:
注意2:使用#符号和$符号的不同
1、配置
pom文件和上个版本一样,只是application.properties新增以下配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
指定了mybatis基础配置文件和实体类映射文件的地址
mybatis-config.xml 配置
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="String" type="java.lang.String" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
2、添加User的映射文件
<mapper namespace="com.example.myproject.dao.TurbineDao" >
<resultMap id="BaseResultMap" type="com.example.myproject.model.Turbine" >
<result column="turbine_code" property="turbineCode" jdbcType="VARCHAR" />
<result column="device_code" property="deviceCode" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
turbineCode, deviceCode
</sql>
<select id="findOne" resultMap="BaseResultMap" parameterType="java.lang.String"> SELECT <include refid="Base_Column_List" /> FROM m_turbine WHERE turbine_code = #{turbineCode}
</select> </mapper>
3.编写dao层代码
public interface TurbineDao { public List<Turbine> findOne(String turbineCode); }
作者:纯洁的微笑
出处:www.ityouknow.com
版权所有,欢迎保留原文链接进行转载:)
标签:启动 .config rom .data you sel dep mysq model
原文地址:https://www.cnblogs.com/mcahkf/p/9599257.html