<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.ttcgroupId>
<artifactId>SSM-demoartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.12version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.6version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.10.RELEASEversion>
dependency>
dependencies>
project>
-- 创建学生信息表
CREATE TABLE `student` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
-- 插入数据
INSERT INTO `boot`.`student` (`id`, `name`, `age`) VALUES ('1', '灰太狼', '22');
INSERT INTO `boot`.`student` (`id`, `name`, `age`) VALUES ('2', '喜羊羊', '16');
package com.ttc.entity;
/**
* 学生类
*/
public class Student {
/**
* id: 编号
*/
private Integer id;
/**
* name: 名字
*/
private String name;
/**
* age: 年龄
*/
private Integer age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.ttc.dao;
import com.ttc.entity.Student;
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 StudentDao {
/**
* 1.查询单个数据
*
* @param id
* @return Student
*/
@Select("select id,name,age from student where id = #{id}")
Student select(Integer id);
/**
* 2.修改单个数据
*
* @param student
*/
@Update("update student set name = #{name},age = #{age} where id = #{id}")
void update(Student student);
/**
* 3.插入单个数据
*
* @param student
*/
@Insert("insert into student(name,age) values (#{name},#{age})")
void insert(Student student);
/**
* 4.删除单个数据
*
* @param id
*/
@Delete("delete from student where id = #{id}")
void delete(Integer id);
/**
* 5.查询所有
* @return List
*/
@Select("select id,name,age from student")
List<Student> getAll();
}
package com.ttc.service;
import com.ttc.entity.Student;
import java.util.List;
public interface StudentService {
/**
* 1.查询单个数据
*
* @param id
* @return Student
*/
Student select(Integer id);
/**
* 2.修改单个数据
*
* @param student
*/
void update(Student student);
/**
* 3.插入单个数据
*
* @param student
*/
void insert(Student student);
/**
* 4.删除单个数据
*
* @param id
*/
void delete(Integer id);
/**
* 5.查询所有
* @return List
*/
List<Student> getAll();
}
package com.ttc.service.impl;
import com.ttc.dao.StudentDao;
import com.ttc.entity.Student;
import com.ttc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
public Student select(Integer id) {
return studentDao.select(id);
}
public void update(Student student) {
studentDao.update(student);
}
public void insert(Student student) {
studentDao.insert(student);
}
public void delete(Integer id) {
studentDao.delete(id);
}
public List<Student> getAll() {
return studentDao.getAll();
}
}
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/boot
jdbc.username=root
jdbc.password=root
package com.ttc.config;
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("com.ttc")
@Import({JdbcConfig.class,MybatisConfig.class})
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {
}
package com.ttc.config;
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.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 ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
package com.ttc.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MybatisConfig {
/**
* 注入sqlSessionFactory
*
* @param dataSource
* @return
*/
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.ttc.entity");
return sqlSessionFactoryBean;
}
/**
* 注入Mapper Scanner
* @return
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.ttc.dao");
return mapperScannerConfigurer;
}
}
package com.ttc;
import com.ttc.config.SpringConfig;
import com.ttc.entity.Student;
import com.ttc.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
StudentService studentService = ctx.getBean(StudentService.class);
// 1.查询
Student student = studentService.select(1);
System.out.println("1.查询单个结果为:" + student);
// 2.修改
Student student2 = new Student();
student2.setAge(999);
student2.setName("懒羊羊");
student2.setId(2);
studentService.update(student2);
System.out.println("2.修改成功");
// 3. 添加
Student student3 = new Student();
student3.setName("沸羊羊");
student3.setAge(18);
studentService.insert(student3);
System.out.println("3.添加成功");
// 4. 删除
studentService.delete(2);
System.out.println("4.删除成功");
// 5.查询所有
List<Student> students = studentService.getAll();
for (Student s : students) {
System.out.println("5.查询所有结果为:" + s);
}
}
}
运行结果
四月 09, 2023 9:21:38 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
1.查询单个结果为:Student{id=1, name='灰太狼', age=22}
2.修改成功
3.添加成功
4.删除成功
5.查询所有结果为:Student{id=1, name='灰太狼', age=22}
5.查询所有结果为:Student{id=5, name='沸羊羊', age=18}
Process finished with exit code 0