环境准备
导依赖
代码语言:javascript复制 <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
jdbc配置文件
代码语言:javascript复制jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hm_test?useSSL=false
jdbc.username=root
jdbc.password=sql123
整合SpringMvc(Servlet配置类,Mvc配置类,Spring配置类)
servlet配置类
代码语言:javascript复制@Configuration
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {SpringConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
// 乱码处理,设置过滤器
@Override
protected Filter[] getServletFilters() {
// 获取字符编码过滤器对象
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
return new Filter[] {characterEncodingFilter};
}
}
Mvc配置类
代码语言:javascript复制@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {
}
Spring配置类
代码语言:javascript复制@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {
}
整合Mybatis(Jdbc配置类,Mybatis配置类)
Jdbc配置类
代码语言:javascript复制@Configuration
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
Mybatis配置类
代码语言:javascript复制@Configuration
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.entity");
sqlSessionFactoryBean.setDataSource(dataSource);
return sqlSessionFactoryBean;
}
@Bean //这个配置会报一个
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.itheima.mapper");
return mapperScannerConfigurer;
}
}
这里会报一个static,是MapperScannerConfigurer是BeanFactoryPostProcessor的一个实现导致的
解决方法是用注解@MapperScan代替
我自己在方法上加了static好像也可以
功能模块(entity,mapper,service,impl)
entity
代码语言:javascript复制public class Source {
private Integer id;
private Integer number;
private String name;
private String subjects;
private Integer gradee;
@Override
public String toString() {
return "Source{"
"id=" id
", number=" number
", name='" name '''
", subjects='" subjects '''
", gradee=" gradee
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubjects() {
return subjects;
}
public void setSubjects(String subjects) {
this.subjects = subjects;
}
public Integer getGradee() {
return gradee;
}
public void setGradee(Integer gradee) {
this.gradee = gradee;
}
}
mapper
代码语言:javascript复制@Mapper
public interface SourceMapper {
@Select("select * from source")
public List<Source> findAll();
}
service
代码语言:javascript复制public interface SourceService {
public List<Source> findAll();
}
impl
代码语言:javascript复制@Service
public class SourceServiceImpl implements SourceService {
@Autowired
private SourceMapper sourceMapper;
@Override
public List<Source> findAll() {
return sourceMapper.findAll();
}
}
测试
数据库测试
测试类
结果
中文乱码测试
接口
代码语言:javascript复制@PostMapping("getincn")
@ResponseBody
public void getInChinese(String name, int age) {
System.out.println("=========================>" name);
System.out.println("=========================>" age);
}