前提环境已搭建,数据库表已创建。(Eclipse + SpringBoot + mybatis + mysql)
1.创建工程
File->new->other->Spring Boot -> Spring Starter Project ->选择jdk等信息 -> 点击Web,选择Web,然后点击SQL,选择JPA、Mybatis、MYSQL,点击next,Finish。
2.文件编辑(以下参考慕课网的视频教程)
2.1修改pom.xml
连接池依赖改为 :
com.mchange
c3p0
0.9.5.1
2.2修改application.properties(src/main/resourcesapplication.properties)
server.port=8082
server.servlet.context-path=/demo
jdbc1.url=jdbc:MySQL://192.168.1.30:30006/数据库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc1.username=数据库访问用户
jdbc1.password=数据库访问密码
jdbc1.driver=com.mysql.jdbc.Driver
mybatis_config_file=mybatis-config.xml
mapper_path=/mapper/**.xml
entity_package=com.lele.demo.entity
2.3mybatis配置(src/main/resources/mybatis-config.xml)
2.4datasource与sessionfactorybean配置
datasource配置(com.lele.demo.config.dao.DataSourceConfiguration.java)
package com.lele.demo.config.dao;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
importcom.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
@Configuration
@MapperScan("com.lele.demo.dao.first")
public class DataSourceConfiguration {
@Value("${jdbc1.driver}")
private String jdbcDriver;
@Value("${jdbc1.url}")
private String jdbcUrl;
@Value("${jdbc1.username}")
private String jdbcUserName;
@Value("${jdbc1.password}")
private String jdbcPassword;
@Bean(name = "dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUserName);
dataSource.setPassword(jdbcPassword);
//关闭连接后不自动commit
dataSource.setAutoCommitOnClose(false);
return dataSource;
}
sessionfactorybean配置(com.lele.demo.config.dao.SessionFactoryConfiguration.java)
package com.lele.demo.config.dao;
import java.io.IOException;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
public class SessionFactoryConfiguration {
@Value("${mybatis_config_file}")
private String mybatisConfigFilePath;
@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 resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
return sqlSessionFactoryBean;
}
}
2.5创建实体类 -- 同默认配置一样
2.6 mapper创建(com.lele.demo.dao.UserDao.java)
package com.lele.demo.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.lele.demo.entity.User;
@Mapper
public interface UserDao {
/** * 根据主键查询*/
User selectUserById(Long id);
}
2.7 mapper.xml创建(src/main/resources/mapper/UserDao.xml)
2.8 service配置:将Dao的接口组合使用,实现事务(TransactionManagementConfiguration)
package com.lele.demo.config.service;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
@Configuration
@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer{
@Autowired
private DataSource dataSource;
@Override
public PlatformTransactionManager annotationDrivenTransactionManager(){
return new DataSourceTransactionManager(dataSource);
}
}
2.9 service接口
package com.lele.demo.service;
import java.util.List;
import com.lele.demo.entity.User;
public interface UserService {
User selectUserById(Long id);
}
2.10 接口实现serviceImpl
package com.lele.demo.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lele.demo.dao.UserDao;
import com.lele.demo.entity.User;
import com.lele.demo.service.UserService;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
@Override
public User selectUserById(Long id) {
// TODO Auto-generated method stub
return userDao.selectUserById(id);
}
}
2.11 Controller层
package com.lele.demo.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.lele.demo.service.UserService;
@RestController
@RequestMapping("/superadmin")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/selectTel", method = RequestMethod.GET)
private Map listTel(){
Map telMap = new HashMap();
Listlist = userService.selectTel();
telMap.put("telMap",list);
return telMap;
}
}
2.12应用入口
package com.lele.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//将spring boot自带的DataSourceAutoConfiguration禁掉,因为它会读取application.properties文件的spring.datasource.*属性并自动配置单数据源
@MapperScan("com.lele.demo.dao")
public class Springboot2Application {
public static void main(String[] args) {
SpringApplication.run(Springboot2Application.class, args);
}
}
完成,运行程序, 打开浏览器,输入http://127.0.0.1:8082/demo/superadmin/selectTel , 返回查询结果 (关于查询get的方法可以用浏览器, post方法要用postMan调用接口测试,淡然get方法页可以用postMan)