- Spring
@SuppressWarnings("all") | |
@Configuration | |
@ComponentScan({"com.dkx.spring.service"}) | |
@PropertySource("classpath:druid.properties") | |
@Import({JdbcConfig.class,MybatisConfig.class}) | |
@EnableTransactionManagement | |
public class SpringConfig { | |
} |
- MyBatis
@SuppressWarnings("all") | |
public class MybatisConfig { | |
@Bean | |
public SqlSessionFactoryBean getSqlSession(DataSource d){ | |
SqlSessionFactoryBean session = new SqlSessionFactoryBean(); | |
session.setTypeAliasesPackage("com.dkx.spring.domain"); | |
session.setDataSource(d); | |
return session; | |
} | |
@Bean | |
public MapperScannerConfigurer getMapper(){ | |
MapperScannerConfigurer mapper = new MapperScannerConfigurer(); | |
mapper.setBasePackage("com.dkx.spring.dao"); | |
return mapper; | |
} | |
} |
@SuppressWarnings("all") | |
public class JdbcConfig { | |
@Value("${jdbc.driver}") | |
private String jdriver; | |
@Value("${jdbc.url}") | |
private String jurl; | |
@Value("${jdbc.username}") | |
private String jusername; | |
@Value("${jdbc.password}") | |
private String jpassword; | |
@Bean | |
public DataSource getDataSource(){ | |
DruidDataSource d = new DruidDataSource(); | |
d.setDriverClassName(jdriver); | |
d.setUrl(jurl); | |
d.setUsername(jusername); | |
d.setPassword(jpassword); | |
return d; | |
} | |
@Bean | |
public PlatformTransactionManager transactionmanager(DataSource d){ | |
DataSourceTransactionManager ds = new DataSourceTransactionManager(); | |
ds.setDataSource(d); | |
return ds; | |
} | |
} |
jdbc.driver=com.mysql.cj.jdbc.Driver | |
jdbc.url=jdbc:mysql://localhost:3306/ssm_db?characterEncoding=utf-8&serverTimezone=UTC | |
jdbc.username=root | |
jdbc.password=dkx |
- SpringMvc
@SuppressWarnings("all") | |
@Configuration | |
@ComponentScan("com.dkx.spring.controller") | |
@EnableWebMvc | |
public class SpringMvcConfig { | |
} |
@SuppressWarnings("all") | |
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 Filter[] getServletFilters() { | |
CharacterEncodingFilter character = new CharacterEncodingFilter(); | |
character.setEncoding("UTF-8"); | |
return new Filter[]{character}; | |
} | |
@Override | |
protected String[] getServletMappings() { | |
return new String[]{"/"}; | |
} | |
} |
- 表与实体类
create database ssm_db; | |
use ssm_db; | |
create table book( | |
id int primary key auto_increment, | |
type varchar(30), | |
name varchar(30), | |
description varchar(30) | |
)character set utf8; | |
insert into book (type, name, description) values ('计算机理论','Spring实战','Spring入门经典教程'); | |
insert into book (type, name, description) values ('计算机理论','Spring高级','Spring高级教程'); | |
insert into book (type, name, description) values ('计算机理论','SpringMvc','SpringMvc入门经典教程'); |
@SuppressWarnings("all") | |
public class Book { | |
private Integer id; | |
private String type; | |
private String name; | |
private String description; | |
public Book() { | |
} | |
public Book(Integer id, String type, String name, String description) { | |
this.id = id; | |
this.type = type; | |
this.name = name; | |
this.description = description; | |
} | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
@Override | |
public String toString() { | |
return "Book{" + | |
"id=" + id + | |
", type='" + type + '\'' + | |
", name='" + name + '\'' + | |
", description='" + description + '\'' + | |
'}'; | |
} | |
} |
- Dao (接口 + 自动代理)
@SuppressWarnings("all") | |
public interface BookDao { | |
public void save(Book b); | |
public void update(Book b); | |
public void delete(Integer id); | |
public Book getById(Integer id); | |
public List<Book> getAll(); | |
} |
<?xml version="1.0" encoding="UTF-8" ?> | |
<!DOCTYPE mapper | |
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | |
"https://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |
<!--namespace 绑定一个对应的 Dao (Mapper) 接口 --> | |
<mapper namespace="com.dkx.spring.dao.BookDao"> | |
<insert id="save" parameterType="com.dkx.spring.domain.Book"> | |
insert into book (type,name,description) values (#{type},#{name},#{description}); | |
</insert> | |
<update id="update" parameterType="com.dkx.spring.domain.Book"> | |
update book set type=#{type},name=#{name},description=#{description} where id=#{id}; | |
</update> | |
<delete id="delete" parameterType="int"> | |
delete from book where id=#{id}; | |
</delete> | |
<select id="getById" parameterType="int" resultType="com.dkx.spring.domain.Book"> | |
select * from book where id=#{id}; | |
</select> | |
<select id="getAll" resultType="com.dkx.spring.domain.Book"> | |
select * from book; | |
</select> | |
</mapper> |
- service (接口 + 实现类)
@SuppressWarnings("all") | |
@Transactional | |
public interface BookService { | |
/** | |
* 保存用户信息 | |
* @param b | |
* @return | |
*/ | |
boolean save(Book b); | |
/** | |
* 更新用户信息 | |
* @param b | |
* @return | |
*/ | |
boolean update(Book b); | |
/** | |
* 按 id 删除用户信息 | |
* @param id | |
* @return | |
*/ | |
boolean delete(Integer id); | |
/** | |
* 按用户 id 查询用户信息 | |
* @param id | |
* @return | |
*/ | |
Book getById(Integer id); | |
/** | |
* 查询所有用户信息 | |
* @return | |
*/ | |
List<Book> getAll(); | |
} |
@Service | |
public class BookServiceImpl implements BookService { | |
@Autowired | |
private BookDao bookDao; | |
@Override | |
public boolean save(Book b) { | |
bookDao.save(b); | |
return true; | |
} | |
@Override | |
public boolean update(Book b) { | |
bookDao.update(b); | |
return true; | |
} | |
@Override | |
public boolean delete(Integer id) { | |
bookDao.delete(id); | |
return true; | |
} | |
@Override | |
public Book getById(Integer id) { | |
return bookDao.getById(id); | |
} | |
@Override | |
public List<Book> getAll() { | |
return bookDao.getAll(); | |
} | |
} |
- controller
@SuppressWarnings("all") | |
@RestController | |
@RequestMapping("/books") | |
public class BookController { | |
@Autowired | |
private BookService bookService; | |
@PostMapping | |
public boolean save(@RequestBody Book b) { | |
return bookService.save(b); | |
} | |
@PutMapping | |
public boolean update(@RequestBody Book b) { | |
return bookService.update(b); | |
} | |
@DeleteMapping("/{id}") | |
public boolean delete(@PathVariable Integer id) { | |
return bookService.delete(id); | |
} | |
@GetMapping("/{id}") | |
public Book getById(@PathVariable Integer id) { | |
return bookService.getById(id); | |
} | |
@GetMapping | |
public List<Book> getAll() { | |
return bookService.getAll(); | |
} | |
} |