下面是一个基于Spring Boot开发的后台管理系统的示例,主要用于管理客户信息,包括登录验证和权限验证,系统登录和退出,客户管理:增、删、改、查等功能。客户信息包括客户单位(包括二级、三级单位),客户姓名、电话、手机、QQ、email。查询将提供多种查询方式,如按客户姓名、单位查询,也可使用模糊查询。查询时采用级联查询。
首先,需要创建一个Spring Boot项目,并添加必要的依赖。在pom.xml文件中添加以下依赖:
xml
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Thymeleaf Template Engine -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
创建数据库表结构如下:
sql
CREATE TABLE customer (
id INT PRIMARY KEY AUTO_INCREMENT,
company_name VARCHAR(255),
customer_name VARCHAR(255),
telephone VARCHAR(255),
mobile VARCHAR(255),
qq VARCHAR(255),
email VARCHAR(255)
);
接下来,创建实体类Customer表示客户信息:
java
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String companyName;
private String customerName;
private String telephone;
private String mobile;
private String qq;
private String email;
// Getters and setters
// ...
}
然后,创建CustomerRepository接口用于与数据库进行交互:
java
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
// 添加自定义的查询方法,如按客户姓名或单位查询,模糊查询等
List<Customer> findByCustomerName(String customerName);
List<Customer> findByCompanyName(String companyName);
List<Customer> findByCustomerNameContaining(String keyword);
List<Customer> findByCompanyNameContaining(String keyword);
}
接下来,创建CustomerService接口和CustomerServiceImpl实现类用于处理客户信息的业务逻辑:
java
@Service
public interface CustomerService {
Customer saveCustomer(Customer customer);
void deleteCustomer(Long customerId);
Customer getCustomer(Long customerId);
List<Customer> getAllCustomers();
List<Customer> searchByName(String keyword);
List<Customer> searchByCompanyName(String keyword);
}
@Service
public class CustomerServiceImpl implements CustomerService {
private final CustomerRepository customerRepository;
public CustomerServiceImpl(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
@Override
public Customer saveCustomer(Customer customer) {
return customerRepository.save(customer);
}
@Override
public void deleteCustomer(Long customerId) {
customerRepository.deleteById(customerId);
}
@Override
public Customer getCustomer(Long customerId) {
return customerRepository.findById(customerId).orElse(null);
}
@Override
public List<Customer> getAllCustomers() {
return customerRepository.findAll();
}
@Override
public List<Customer> searchByName(String keyword) {
return customerRepository.findByCustomerNameContaining(keyword);
}
@Override
public List<Customer> searchByCompanyName(String keyword) {
return customerRepository.findByCompanyNameContaining(keyword);
}
}
接下来,创建CustomerController控制器类用于处理客户信息的请求和响应:
java
@Controller
@RequestMapping("/customers")
public class CustomerController {
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping("/")
public String getAllCustomers(Model model) {
List<Customer> customers = customerService.getAllCustomers();
model.addAttribute("customers", customers);
return "customer-list";
}
@GetMapping("/add")
public String addCustomerForm(Model model) {
model.addAttribute("customer", new Customer());
return "customer-add";
}
@PostMapping("/add")
public String addCustomerSubmit(@ModelAttribute("customer") Customer customer) {
customerService.saveCustomer(customer);
return "redirect:/customers/";
}
@GetMapping("/edit/{id}")
public String editCustomerForm(@PathVariable("id") Long customerId, Model model) {
Customer customer = customerService.getCustomer(customerId);
model.addAttribute("customer", customer);
return "customer-edit";
}
@PostMapping("/edit/{id}")
public String editCustomerSubmit(@PathVariable("id") Long customerId, @ModelAttribute("customer") Customer customer) {
customer.setId(customerId);
customerService.saveCustomer(customer);
return "redirect:/customers/";
}
@GetMapping("/delete/{id}")
public String deleteCustomer(@PathVariable("id") Long customerId) {
customerService.deleteCustomer(customerId);
return "redirect:/customers/";
}
@GetMapping("/search")
public String searchCustomers(@RequestParam("keyword") String keyword, Model model) {
List<Customer> customers = customerService.searchByName(keyword);
model.addAttribute("customers", customers);
model.addAttribute("keyword", keyword);
return "customer-search";
}
// 其他处理增删改查等操作的方法
// ...
}
在resources目录下创建templates文件夹,然后在templates下创建customer-list.html,customer-add.html,customer-edit.html,customer-search.html等Thymeleaf模板文件。
最后,创建SecurityConfig类用于配置登录验证和权限验证:
java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/customers/")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password("{noop}admin").roles("ADMIN");
}
}
以上代码只是一个简单示例,还需要根据具体业务需求进行适当调整和完善。完成以上代码后,启动Spring Boot应用程序,您将能够在浏览器中访问管理系统,并使用登录验证和权限验证来管理客户信息。