//數據源 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"
#數據庫鏈接信息 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
掃描mapper包java
@MapperScan("com.example.datas.manager.mapper")
@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(); } }
@Data @NoArgsConstructor @ToString public class Student { private Integer id; private String name; private Integer age; private Integer sex; }
public interface StudentMapper { @Insert("insert into c_student (name, age, sex) values (#{name}, #{age}, #{sex})") Integer insert(Student student); }
public interface StudentService { /** * 新增學生 * @author FuZizheng * @date 2018/2/28 下午5:40 * @param: [student] * @return: java.lang.Integer */ Integer insert(Student student); }
@Service public class ServiceStudentImpl implements StudentService { @Autowired private StudentMapper studentMapper; @Override public Integer insert(Student student) { return studentMapper.insert(student); } }
@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); } }
<?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>
未配置成功能夠在下面留言~我會一一解答~mysql