整合中Tomcat等配置与前面学习相同
1.添加依赖↓
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--选择与tomcat对应的servlet-api版本-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>10.1.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
各配置类如下:
JdbcConfig↓
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JdbcConfig {
@Value("${jdbc.drive}")
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 druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}
MyBatisConfig↓
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("dao");
return mapperScannerConfigurer;
}
}
SpringConfig↓
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan({"dao", "service"})
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
/*@ComponentScan(value = "com.example.bishe",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
))*/
public class SpringConfig {
}
SpringMvcConfig↓
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan("controller")
@EnableWebMvc
public class SpringMvcConfig {
}
ServletContainersInitConfig↓
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletContainersInitConfig 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[]{"/"};
}
//POST传中文字符串时出现乱码添加过滤
/*@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}*/
}
jdbc.properties的配置如下↓
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false
jdbc.username=root
jdbc.password=root
public class User {
private String uName;
private String uSex;
private String uPhone;
private String uIdentityCard;
private String uPassword;
private String uAccount;
@Override
public String toString() {
return "User{" +
"uName='" + uName + '\'' +
", uSex='" + uSex + '\'' +
", uPhone='" + uPhone + '\'' +
", uIdentityCard='" + uIdentityCard + '\'' +
", uPassword='" + uPassword + '\'' +
", uAccount='" + uAccount + '\'' +
'}';
}
public void setuName(String uName) {
this.uName = uName;
}
public void setuSex(String uSex) {
this.uSex = uSex;
}
public void setuPhone(String uPhone) {
this.uPhone = uPhone;
}
public void setuIdentityCard(String uIdentityCard) {
this.uIdentityCard = uIdentityCard;
}
public void setuPassword(String uPassword) {
this.uPassword = uPassword;
}
public void setuAccount(String uAccount) {
this.uAccount = uAccount;
}
public String getuName() {
return uName;
}
public String getuSex() {
return uSex;
}
public String getuPhone() {
return uPhone;
}
public String getuIdentityCard() {
return uIdentityCard;
}
public String getuPassword() {
return uPassword;
}
public String getuAccount() {
return uAccount;
}
}
import domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface UserDao {
@Insert("insert into user(uAccount,uPassword) values(#{uAccount},#{uPassword})")
public void save(User user);
@Update("update user set uName = #{uName}, uSex = #{uSex}, uPhone = #{uPhone}, uIdentityCard= #{uIdentityCard} where uAccount = #{uAccount}")
public void update(User user);
@Update("update user set uPassword = #{uPassword} where uAccount = #{uAccount}")
public void updatePassword(String uAccount);
@Delete("delete from user where uAccount = #{uAccount}")
public void delete(String uAccount);
@Select("select * from user where uAccount = #{uAccount}")
public User getByUaccount(String uAccount);
@Select("select * from user")
public List<User> getAll();
}
import domain.User;
import java.util.List;
public interface UserService {
/**
*保存
* @param user
* @return
*/
public boolean save(User user);
/**
*修改信息
* @param user
* @return
*/
public boolean update(User user);
/**
*修改密码
* @param user
* @return
*/
public boolean updatePassword(String uAccount);
/**
*删除
* @param user
* @return
*/
public boolean delete(String uAccount);
/**
*查询
* @param user
* @return
*/
public User getByuAccount(String uAccount);
/**
*查询全部
* @param user
* @return
*/
public List<User> getAll();
}
import dao.UserDao;
import domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.UserService;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public boolean save(User user) {
userDao.save(user);
return true;
}
@Override
public boolean update(User user) {
userDao.update(user);
return true;
}
@Override
public boolean updatePassword(String uAccount) {
userDao.updatePassword(uAccount);
return true;
}
@Override
public boolean delete(String uAccount) {
userDao.delete(uAccount);
return true;
}
@Override
public User getByuAccount(String uAccount) {
return userDao.getByUaccount(uAccount);
}
@Override
public List<User> getAll() {
return userDao.getAll();
}
}
import domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.UserService;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/save")
public String save(){
System.out.println("你好");
return "[success]";
}
@PostMapping
public boolean save(@RequestBody User user) {
return userService.save(user);
}
@PutMapping
public boolean update(@RequestBody User user) {
return userService.update(user);
}
@PutMapping("/{uAccount}")
public boolean updatePassword(@PathVariable String uAccount) {
return userService.updatePassword(uAccount);
}
@DeleteMapping("/{uAccount}")
public boolean delete(@PathVariable String uAccount) {
return userService.delete(uAccount);
}
@GetMapping("/{uAccount}")
public User getByAccount(@PathVariable String uAccount) {
return userService.getByuAccount(uAccount);
}
@GetMapping
public List<User> getAll() {
return userService.getAll();
}
}
4.测试
测试时Junit4中文乱码,未找到解决方法,就此不记
通过SpringMVC控制器功能进行测试↓
注:实体类的属性名需与数据库中字段名相同,否的读取数据时,无法装载进实体类对象中
5.事务管理
SpringConfig中添加事务管理注解@EnableTransactionManagement↓
JdbcConfig配置类中添加平台事务管理类型方法,其中DataSource是自动装配的Bean↓
服务类接口中使用注解@Transactional开启事务管理↓