SpringBoot_集成MyBatis(XML方式)

来源:互联网 发布:新疆广电网络缴费方式 编辑:程序博客网 时间:2024/06/06 09:21
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.pojomybatis.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接口:
@Mapperpublic 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
@Servicepublic 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
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

如有侵权,请联系本人。











原创粉丝点击