Springboot+mybatis配置(注解/.xml)

1.gradle

//数据源
compile 'com.alibaba:druid-spring-boot-starter:1.1.8'
compile 'mysql:mysql-connector-java:5.1.38'
//配置mybatis
compile "org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1"

2.application.properties

#数据库连接信息
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/takin_write
spring.datasource.username=MurasakiSeiFu
spring.datasource.password=123456

#打印sql信息
logging.level.com.example.datas.manager.mapper=debug

3.启动类添加注解

扫描mapper包

@MapperScan("com.example.datas.manager.mapper")

4.MyBatis 不含动态数据源的java配置

@Configuration
@MapperScan(basePackages = {MyBatisConfig.MAPPER_PACKAGE}, sqlSessionFactoryRef = MyBatisConfig.SESSIONFACTORY_NAME)
public class MyBatisConfig {

    /**SqlSessionFactory名称.*/
    public final static String SESSIONFACTORY_NAME = "sqlSessionFactory";
    /**mapper包路径,必须与其他SqlSessionFactory-mapper路径区分.*/
    public final static String MAPPER_PACKAGE = "com.example.datas.manager.mapper";
    /**mapper.xml文件路径,必须与其他SqlSessionFactory-mapper路径区分.*/
    public final static String MAPPER_XML_PATH = "classpath:mapper/*.xml";

    @Autowired
    private DataSourceProperties dataSourceProperties;


    @Bean(name = "dataSource")
    public DataSource dataSource() {
        //建议封装成单独的类
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dataSourceProperties.getUrl());
        System.err.println(dataSourceProperties.getUrl());
        dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
        dataSource.setUsername(dataSourceProperties.getUsername());
        dataSource.setPassword(dataSourceProperties.getPassword());

        return dataSource;

    }

    //默认Bean首字母小写,简化配置 
    //将SqlSessionFactory作为Bean注入到Spring容器中,成为配置一部分。
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_XML_PATH));
        return sqlSessionFactoryBean.getObject();
    }

}

5.实体

@Data
@NoArgsConstructor
@ToString
public class Student {

    private Integer id;

    private String name;

    private Integer age;

    private Integer sex;
}

6.注解方式 mapper

public interface StudentMapper {

    @Insert("insert into c_student (name, age, sex) values (#{name}, #{age}, #{sex})")
    Integer insert(Student student);
}

7.service层

public interface StudentService {

    /**
     * 新增学生
     * @author FuZizheng
     * @date 2018/2/28 下午5:40
     * @param: [student]
     * @return: java.lang.Integer
     */
    Integer insert(Student student);
}

8.Impl

@Service
public class ServiceStudentImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Integer insert(Student student) {
        return studentMapper.insert(student);
    }
}

9.controller

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 新增
     *
     * @author MurasakiSeiFu
     * @date ` 上午11:46
     * @param: []
     * @return: java.lang.Integer
     */
    @PostMapping("/insert")
    public Integer insert() {

        Student student = new Student();
        student.setName("hehe");
        student.setAge(44);
        student.setSex(1);
        return studentService.insert(student);
    }
}

10.xml 比较基础啦,网上也比较多,因为我们在上面的MyBatis配置里已经配置并加载xml的扫描路径,这里就只帖出一个基础的xml文件,在resources/mapper/ 下

<?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.suixingpay.datas.manager.mapper.StudentMapper">
    <resultMap id="StudentResultMap" type="com.suixingpay.datas.manager.entity.Student">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, name, age, sex
    </sql>

    <select id="findAllByXml" resultMap="StudentResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        c_student
    </select>

</mapper>

未配置成功可以在下面留言~我会一一解答~

    原文作者:SQL
    原文地址: https://segmentfault.com/a/1190000013468611
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞