Mybatis-Spring connection MySQL8.0 configuration steps error solution

Environment and configuration files

  • JDBC jar version: 8.0.11
  • Mybatis jar version: 3.4.6
  • Spring jar version: 4.3.18
  • Mybatis-Spring jar version: 1.3.1
  • Configuration information file: db.properties
  • Spring configuration file: applicationContext.xml
  • The test path is as shown below
    Insert picture description here

Configure db.properties

  • Fill in the following content in the configuration db.properties

    # 驱动名这样写
    jdbc.driver=com.mysql.cj.jdbc.Driver
    # url这样写
    jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
    # 数据库用户名
    jdbc.username=root
    # 数据密码
    jdbc.password=password 
    
    • There is a mistake here, that is, username is used as the key of the user name. In this case, ${username} is used when configuring the data source in applicationContext.xml. This will cause a problem, because the XML expression ${ username}, represents the username under the computer environment path!!! That is to say, the username of your computer is used, not the username of the database!!! In short, don't use username as the key name. I use jdbc.username.

Configure applicationContext.xml

  1. In applicationContext.xml, import the db.properties file.
    <context:property-placeholder location="db.properties"/>
    
  2. Configure data source
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
  3. Configure Mybatis to scan the mapper.XML file
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 写一些数据库的配置, 因为我没用到, 所以用不着 -->
        <!-- <property name="configLocation" value="sqlMapConfig.xml"/> -->
        <property name="mapperLocations" value="com/dao/mapper/*.xml"/>
    </bean>
    
  4. Scan all dao layer interfaces
    <!-- 扫描所有dao -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao" />
        <!-- 单数据源可以不写sqlSessionFactoryBeanName属性 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
    

Configure dao layer interface and mapper file

  1. dao interface
    public interface UserDao {
          
          
    	public List<User> selAll();
    }
    
  2. mapperXML file
    <mapper namespace="com.dao.UserDao">
    	<select  id="selAll" resultType="com.entity.User">
        	select * from user
    	</select>
    </mapper>
    

test

@Test
public void selAll() {
    
    
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   UserDao userDao = context.getBean(UserDao.class);
   List<User> list = userDao.selAll();
   System.out.println(list);
}

Guess you like

Origin blog.csdn.net/qq_39906884/article/details/84435888