This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

1. Create complex objects

Complex objects: There are no constructors in a class, or constructors cannot be called such as interface types or abstract class instances

  //1. Class implements FactoryBean
  public class ConnectionFactoryBean implements FactoryBean<Connection> {
    @Override
    public Connection getObject(a) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/test"."root"."root");
    }

    @Override
    publicClass<? > getObjectType() {return Connection.class;
    }

    @Override
    public boolean isSingleton(a) {
        return false; }}// 2. Configure factory management
  <bean id="connectionFactoryBean" class="com.baizhi.factorybean.ConnectionFactoryBean"></bean>

  // 3. Get complex objects
  ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  Connection conn = (Connection) context.getBean("connectionFactoryBean");
  System.out.println(conn);
Copy the code


2.SM integrates ideas

1. What the Spring framework does

The Spring Framework project management framework is responsible for the creation, use, and destruction of component objects in a project

2. The role of Mybatis framework

Mybatis framework persistence layer framework is mainly used to simplify the operation of database access

3. Integrate your thoughts

Mybatis is a framework that takes over the creation of core objects in Mybatis

4. What are the core objects in Mybatis

SqlSessionFactory directly depends on Mybatis -config. XML file, and must depend on data source object and mapper file registration

The integration is the creation of the SqlSessionFactory object through the Spring factory management

5. Integrate thought diagrams

3. SM integrates DAO programming steps

1. Introduce mybatis dependent JAR package

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.28.</version>
</dependency>
Copy the code

2. Introduce Spring related JAR packages

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.32..RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.32..RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.32..RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.32..RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId>  <version>4.32..RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.32..RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>4.32..RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.32..RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>4.32..RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>  <version>4.32..RELEASE</version>
</dependency>
Copy the code

3. Mybatis jar – spring integration

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.31.</version>
</dependency>
Copy the code

4. Introduce the database driver JAR

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.140.</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.112.</version>
</dependency>
Copy the code

5. Build table

6. Write entity classes

7. Write DAO interfaces

public interface UserDAO {
    // Query all
    List<User> findAll(a);
}
Copy the code

8. Compile a Mapper configuration file

<! DOCTYPE mapper PUBLIC"- / / mybatis.org//DTD Mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.dao.UserDAO">
    
    <select id="findAll" resultType="com.xxx.entity.User">
        select id,name,age,bir from t_user
    </select>

</mapper>
Copy the code

9. Write the spring-Myabtis integration configuration file

Core idea: take over the creation of SqlSessionFactory DAO object in Mybatis through Spring

Analysis 1: Creating the SqlSessionFactory depends on the location of the mapper configuration file of the data source object

Analysis 2: Create DAO object need to rely on SqlSessionFactory and DAO interface type

<! Create data source object --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/sm"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/> </bean> <! SqlSessionFactory = sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/ > <! -- Insert mapper configuration file location --> <property name="mapperLocations" value="classpath:com/xxx/mapper/*.xml"></property> <! - names Default alias for the name of the class of | class name first letter lowercase - > < property name ="typeAliasesPackage" value="com.xxx.entity"/> </bean> <! Create DAO object --> <bean id="userDAO" class="org.mybatis.spring.mapper.MapperFactoryBean"> <! SqlSessionFactory--> <property name="sqlSessionFactory" ref="sqlSessionFactory"/ > <! Inject the DAO interface into the package --> <property name="mapperInterface" value="com.xxx.dao.UserDAO"/>
    </bean>
Copy the code

10. Start factory tests

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
UserDAO userDAO = (UserDAO) context.getBean("userDAO");
List<User> users = userDAO.findAll();
for (User user : users) {
  System.out.println(user);
}
Copy the code


3.SM integrates Service programming steps

1. Compile the spring-Mybatis. XML configuration file

<! Create transaction manager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/> </bean> <! -- the tx namespace tx:advice is used to automatically convert the transaction manager into a notification object around the notification transaction-manager: used to specify who the external transaction manager is --> <tx:advice ID ="txAdvice" transaction-manager="transactionManager"> <! -- Configure business layer method transaction fine-grained configuration --> <tx: Attributes > <tx:method name="save*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="find*"/> </tx:attributes> </tx:advice> <! < AOP :config> < AOP :pointcut id="pc" expression="within(com.xxx.service.*)"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>
Copy the code

2. Configuration icon

3. Create a Service interface

public interface UserService {
    List<User> findAll();
}
Copy the code

4. Create a Service object

public class UserServiceImpl implements UserService {
    
    private UserDAO userDAO;
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Override
    public List<User> findAll(a) {
        returnuserDAO.findAll(); }}Copy the code

5. Configure service component objects

<! Configure the Service component --> <bean id="userService" class="com.xxx.service.UserServiceImpl">
        <property name="userDAO" ref="userDAO"/>
    </bean>
Copy the code

6. Start the test