From f15d3b1dfa9c573e3e170bd0f69bfba76fb8a273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=AE=8F=E8=BE=BE?= <2657224306@qq.com> Date: Mon, 1 Jan 2024 23:03:46 +0800 Subject: [PATCH] zuoye --- .../1231.md" | 265 ++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 "48 \351\251\254\345\256\217\350\276\276/1231.md" diff --git "a/48 \351\251\254\345\256\217\350\276\276/1231.md" "b/48 \351\251\254\345\256\217\350\276\276/1231.md" new file mode 100644 index 0000000..f8cee3c --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/1231.md" @@ -0,0 +1,265 @@ +# 复习 + +### JdbcConfig + +```java +package com.mdd.config; + +import com.alibaba.druid.pool.DruidDataSource; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +public class JdbcConfig { + @Bean + public DataSource dataSource(){ + DruidDataSource ds = new DruidDataSource(); + ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); + ds.setUrl("jdbc:mysql:///masql"); + ds.setUsername("root"); + ds.setPassword("123456"); + return ds; + } +} + +``` + +### MybatisConfig + +```java +package com.mdd.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 { + @Bean + public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource){ + SqlSessionFactoryBean ssfy = new SqlSessionFactoryBean(); + ssfy.setTypeAliasesPackage("com.mdd.domain"); + ssfy.setDataSource(dataSource); + return ssfy; + } + @Bean + public MapperScannerConfigurer mapperScannerConfigurer(){ + MapperScannerConfigurer mc = new MapperScannerConfigurer(); + mc.setBasePackage("com.mdd.mapper"); + return mc; + } +} + +``` + +### SpringConfig + +```java +package com.mdd.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@ComponentScan("com.mdd") +@Import({JdbcConfig.class,MybatisConfig.class}) +public class SpringConfig { + +} + +``` + +### User + +```java +package com.mdd.domain; + +public class User { + private Integer id; + private String name; + private String sex; + private Integer age; + + public User() { + } + + public User(Integer id, String name, String sex, Integer age) { + this.id = id; + this.name = name; + this.sex = sex; + this.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 String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + ", age=" + age + + '}'; + } +} + +``` + +### UserMapper + +```java +package com.mdd.mapper; + +import com.mdd.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 UserMapper { + @Select("select * from tb_ures") + List findAll(); + + @Select("select * from tb_ures where id=#{id}") + User findByid(int id); + + @Delete("delete from tb_ures where id=#{id}") + void deleteByid(int id); + + @Update("Updata tb_ures set name=#{name},sex=#{sex},age=#{age} where id=#{id}") + void updetaByid(User user); + + @Insert("insert into tb_ures(name,sex,age) values(#{name},#{sex},#{age})") + void insertByid(User user); +} + +``` + +### App 测试类 + +```java +import com.mdd.config.SpringConfig; +import com.mdd.domain.User; +import com.mdd.mapper.UserMapper; +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); + UserMapper bean = ctx.getBean(UserMapper.class); + List all = bean.findAll(); + System.out.println(all); + } +} + +``` + +### 架包 + +```xml + + + 4.0.0 + + com.mdd + spring_10_review + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + org.springframework + spring-context + 5.2.25.RELEASE + + + + org.springframework + spring-jdbc + 5.2.25.RELEASE + + + org.springframework + spring-test + 5.2.25.RELEASE + + + + org.mybatis + mybatis + 3.5.13 + + + + org.mybatis + mybatis-spring + 2.0.5 + + + + com.mysql + mysql-connector-j + 8.1.0 + + + + junit + junit + 4.13.2 + test + + + + com.alibaba + druid + 1.1.23 + + + + + +``` -- Gitee