新建项目,选择依赖
这种依赖不是springboot官方出的,官方是spring开头
引入druid数据源
1、配置druid数据源
2、引入sql文件建表,注意classpath:后没有空格
创建完表后记得注释掉上边schema部分,否则每次程序启动都会重新创建表
引入log4j.properties以及log4j依赖
<!--引入log4j依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
3、创建javaBean封装数据
//指定操作数据库的mapper
@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Update("update department set departmentname=#{departmentName} where id=#{id}")
public int updateDept(Department department);
//使用自动生成的主键,并告知bean中哪个属性封装主键,这样查询结果id也有了
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("insert into department(departmentname) values(#{departmentName})")
public int insertDept(Department department);
}
controller
@RestController
public class DeptController {
@Autowired
DepartmentMapper departmentMapper;
@GetMapping("/dept/{id}")
public Department getDepartment(@PathVariable("id") Integer id){
return departmentMapper.getDeptById(id);
}
@GetMapping("/dept")
public Department insertDept(Department department){
departmentMapper.insertDept(department);
return department;
}
}
当我改变表的column为以下形式
会发现封装不上
需要自定义mybatis配置,给容器中添加一个ConfigurationCustomizer;
//mybatis自定义配置
@Configuration
public class MybatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
//开启驼峰命名法映射规则
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
这时可以查出
也可以直接在application.yml中配置,就不需要定义配置类了
这样就不需要在每个mapper都加注解了
@MapperScan(value = "com.sjg.springboot.mapper")
@SpringBootApplication
public class Springboot06DataMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot06DataMybatisApplication.class, args);
}
}
employeeMapper.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.sjg.springboot.mapper.EmployeeMapper">
<select id="getEmployeeById" parameterType="int" resultType="com.sjg.springboot.bean.Employee">
SELECT * from employee where id=#{id}
</select>
<insert id="insertEmp" parameterType="com.sjg.springboot.bean.Employee">
insert into employee(lastname, email, gender, d_id) values(#{lastName}, #{email}, #{gender}, #{dId})
</insert>
</mapper>
配置文件路径
这里之前的驼峰设置要注释,否则报错,config和config-location不能同时出现
需要在mybatis配置文件中设置驼峰命名
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>