springboot整合mybatis需要依赖org.mybatis.spring.boot,相应数据库的jdbc包当然也是要加的
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
然后在springboot的配置文件中加入数据库连接的相关信息:
spring.datasource.url=jdbc:mysql://192.168.1.207:8066/test?characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
数据库访问的代码:
@Mapper
public interface CustomMapper {
@Select("select * from custom where id=#{id}")
public Custom findById(@Param("id")long id);
}
如果是用xml的也是可以的,不需要额外配置:
@Mapper
public interface CustomMapper {
// @Select("select * from custom where id=#{id}")
public Custom findById(/*@Param("id")*/long id);
}
<mapper namespace="org.lhc.myspjar.dao.CustomMapper">
<select id="findById" resultType="org.lhc.myspjar.po.Custom" parameterType="long">
select * from custom where id=#{id}
</select>
</mapper>
转载请注明出处:http://blog.youkuaiyun.com/redstarofsleep