1.导入jar包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.3.1</version>
</dependency>
2.applicationContext.xml
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.pro.domain"/>
</bean>
3.XxDao接口
public interface XxDao extends BaseMapper<Xx> {
// public void insertXx(Xx xx);
//
// public List<Xx> selectXx();
//
// public Xx selectXxById(Integer id);
//
// public void updateXx(Xx xx);
}
注释掉原来的代码,继承
XxDao.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.pro.dao.XxDao">
<!-- <insert id="insertXx" parameterType="xx" useGeneratedKeys="true" keyColumn="id" keyProperty="id">-->
<!-- insert into xx(content) values (#{content})-->
<!-- </insert>-->
<!-- <select id="selectXx" resultType="xx">-->
<!-- select*from xx-->
<!-- </select>-->
<!-- <select id="selectXxById" parameterType="int" resultType="xx">-->
<!-- select * from xx where id=#{id}-->
<!-- </select>-->
<!-- <update id="updateXx" parameterType="com.pro.domain.Xx">-->
<!-- update xx set content=#{content} where id=#{id}-->
<!-- </update>-->
</mapper>
4.实体类
@TableName("xx")
public class Xx {
//表的id是自动增长的
@TableId(type = IdType.AUTO)
@TableField("id")
private Integer id;
@TableField("content")
private String content;
数据库和实体类的属性一一对应
5.删掉service和controller
6.测试类增删改查
package com.pro.dao;
import com.pro.domain.Xx;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class XxDaoTest {
@Autowired
private XxDao xxDao;
@Test
public void testInsert() {
Xx xx = new Xx("wdq");
xxDao.insert(xx);
}
@Test
public void testRemove(){
xxDao.deleteById(6);
}
@Test
public void testModify(){
Xx xx = xxDao.selectById(8);
xx.setContent("hahhahahhah");
xxDao.updateById(xx);
}
}