1.创建一个springboot项目,在pom中导入才c3p0,mybatis,web的依赖
2.编写application.properties文件
server.port=8082
server.context-path=/demo
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456
mybatis_config_file=mybatis-config.xml
mapper_path=/mapper/**.xml
entity_package=com.xdl.demo.entiy
3.编写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="useGeneratedKeys" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
4.编写扫描mybatis类DataSourceConfiguration
package com.xdl.demo.config.dao;
@Configuration
//配置mybatis mapping的扫描
@MapperScan("com.xdl.demo.dao")
public class DataSourceConfiguration {
@Value("{jdbc.url}")
private String jdbcUrl;
@Value("{jdbc.password}")
private String Password;
@Bean(name="dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(Username);
dataSource.setPassword(Password);
dataSource.setAutoCommitOnClose(false);//关闭连接后不自动commit
return dataSource;
}
}
6.编写扫描文件类SessionFactoryConfiguration
package com.xdl.demo.config.dao;
@Configuration
public class SessionFactoryConfiguration {
@Value("{mapper_path}")
private String mapperPath;
@Value("${entity_package}")
private String entityPackage;
@Autowired
@Qualifier("dataSource")//按照名字加载进来
private DataSource dataSource;
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));
PathMatchingResourcePatternResolver resolever=new PathMatchingResourcePatternResolver();
String packageSearchPath=PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX+mapperPath;
sqlSessionFactoryBean.setMapperLocations(resolever.getResources(packageSearchPath));
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
return sqlSessionFactoryBean;
}
}
7.编写service配置类
package com.xdl.demo.config.service;
@Configuration
@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer{
@Autowired
private DataSource dataSource;
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
8.编写实体类
package com.xdl.demo.entity;
public class Area {
private Integer areaId;
private String areaName;
private Integer priority;//权重
private Date createTime;
private Date lastEditTime;
public Integer getAreaId() {
return areaId;
}
public void setAreaId(Integer areaId) {
this.areaId = areaId;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastEditTime() {
return lastEditTime;
}
public void setLastEditTime(Date lastEditTime) {
this.lastEditTime = lastEditTime;
}
}
9.编写dao接口
package com.xdl.demo.dao;
public interface AreaDao {
/**
* 查询所有地区的列表
* @return
*/
List<Area> selectArea();
}
10.编写mapping的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.xdl.demo.dao.AreaDao">
<select id="selectArea" resultType="com.xdl.demo.entity.Area">
select * from tb_area order by priority desc
</select>
</mapper>
11.编写Controller类
package com.xdl.demo.controller;
@RestController
@RequestMapping("/admin")
public class AreaController {
@Autowired
private AreaService areaService;
@RequestMapping(value="/listArea",method=RequestMethod.GET)
private Map<String,Object> listArea(){
Map<String,Object> modelMap=new HashMap<String,Object>();
List<Area> list=areaService.selectArea();
modelMap.put("areaList", list);
return modelMap;
}
}
12.编写同一异常处理类
package com.xdl.demo.handler;
@ControllerAdvice
public class GlobaleceptionHandler {
@ExceptionHandler(value=Exception.class)
@ResponseBody
public Map<String,Object> exceptionHandler(HttpServletRequest request,Exception e){
Map<String,Object> modelMap=new HashMap<String,Object>();
modelMap.put("success", false);
modelMap.put("errMsg", e.getMessage());
return modelMap;
}
}
13.编写Service接口
package com.xdl.demo.service;
public interface AreaService {
/**
* 查询所有地区的列表
* @return
*/
List<Area> selectArea();
}
14.编写Service实现类
package com.xdl.demo.serviceImpl;
@Service
public class AreaServiceImpl implements AreaService {
@Autowired
private AreaDao areaDao;
@Override
public List<Area> selectArea() {
return areaDao.selectArea();
}
}
网友评论