SpringBoot_集成MyBatis(XML方式)

1.pom.xml配置
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <!-- 请不要使用1.0.0版本,因为还不支持拦截器插件,使用最新版本即可 --> <version>1.0.1-SNAPSHOT</version> </dependency>



2.在application.properties添加配置

mybatis.type-aliases-package:com.chinac.ccs.mirror.pojo
mybatis.mapper-locations:classpath:/com/chinac/ccs/mirror/mapper/mapper*.xml

或者:
#mybatis配置
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:mappers/*Mapper.xml
#entity扫描的包名
mybatis.type-aliases-package=com.example.entity

3.实体类
public class Mirror  implements Serializable{
        private long id;
        private String name;
//省略getter、setter方法
}


4.mapper映射:方法名字要和映射文件中的ID一致,这样就可以自动匹配

Dao接口:
@Mapper
public interface MirrorDao {
    public void add(Mirror mirror);

   public Mirror findById(@Param(value="id")long id);

   public void delete(@Param(value="id")long id);

   public void update(Mirror mirror);

   public List<Mirror> list(@Param(value="name")String name);
}

映射文件:
<?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.chinac.ccs.mirror.dao.MirrorDao">  

  <resultMap type="com.chinac.ccs.mirror.pojo.Mirror" id="BaseResultMap">
      <id column="id" property="id" />
      <result column="name" property="name" />
  </resultMap>

  <select id="findById" resultMap="BaseResultMap">
      SELECT * FROM mirror WHERE id = #{id}
  </select>

  <insert id="add" parameterType="com.chinac.ccs.mirror.pojo.Mirror"
      keyProperty="id" useGeneratedKeys="true">
      insert into mirror
      <trim prefix="(" suffix=")" suffixOverrides=",">
          id,name,
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides=",">
          #{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},
      </trim>
  </insert>

  <select id="delete">
      delete from mirror where 1=1
      <if test="id != null">
          and id = #{id}
        </if>
  </select>

  <update id="update" parameterType="com.chinac.ccs.mirror.pojo.Mirror">
      update mirror 
      <set>
          <if test="name != null">
              name = #{name,jdbcType=VARCHAR},
            </if>
      </set>
      where id = #{id}
  </update>

  <select id="list" resultMap="BaseResultMap">
      select * from mirror where 1=1
      <if test="name != null ">
          and <![CDATA[name like CONCAT('%',#{name,jdbcType=VARCHAR},'%' )]]>
      </if>
  </select>
</mapper>

5.service
@Service
public class MirrorService {
    @Autowired
    private MirrorDao mirrorDao;

    public Mirror findById(Long id){
        return mirrorDao.findById(id);
    }
}

以上一些配置应该就能够正常运行了,
最后再说下,
如果你使用的IDEA,有些用户没有进行配置,
Classes编译中的代码内,是没有XML文件的,
也就是mapper找不到映射的xml文件,
所以也就需要进行配置。

下面一些配置如果不存在xml不能编译问题可以进行忽略

在pom.xml文件中进行配置
解决办法就是增加配置   
<build>
<plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>copy-xmls</id>
            <phase>process-resources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
                        <outputDirectory>${basedir}/target/classes</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/java</directory>
                                <includes>
                                    <include>**/*.xml</include>
                                </includes>
                            </resource>
                        </resources>
                 </configuration>
          </execution>
 
        </executions>
      </plugin>
</plugins>
</build>
 
这样配置之后    idea在builde的时候   或者执行   maven  test  的时候   才能把源码文件夹里的xml文件与java文件一起搬到target/classes   里面去    
别人如果导入你的这个maven工程   也不用设置idea了



以上内容,转自http://www.jianshu.com/p/a811a89d1b28  


作者:perfect_jimmy
链接:http://www.jianshu.com/p/a811a89d1b28
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

如有侵权,请联系本人。











  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值