其他分享
首页 > 其他分享> > mybatis和spring的整合

mybatis和spring的整合

作者:互联网

7.1、Mybatis步骤

  1. 编写实体类

    public class User {
    
        private String userCode;
        private String userName;
        private String phone;
        private String address;
    
        public User() {
        }
    
        public User(String userCode, String userName, String phone, String address) {
            this.userCode = userCode;
            this.userName = userName;
            this.phone = phone;
            this.address = address;
        }
    
        public String getUserCode() {
            return userCode;
        }
    
        public void setUserCode(String userCode) {
            this.userCode = userCode;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "userCode='" + userCode + '\'' +
                    ", usrName='" + userName + '\'' +
                    ", phone='" + phone + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }
    
  2. 编写核心配置文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <properties resource="jdbc.properties"/>
    
        <settings>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
            <setting name="cacheEnabled" value="true"/>
        </settings>
    
        <typeAliases>
            <typeAlias type="com.mhy.pojo.User" alias="user"/>
        </typeAliases>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${driver}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper class="com.mhy.mapper.UserMapper"/>
        </mappers>
    
    </configuration>
    
  3. 编写接口

    public interface UserMapper {
    
        List<User> queryAllUser();
    
    }
    
  4. 编写Mapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    
    
    <mapper namespace="com.mhy.mapper.UserMapper">
        <select id="queryAllUser" resultType="user">
            select user_code,user_name,phone,address from t_user;
        </select>
    </mapper>
    
  5. 测试

    public class UserMapperTest {
    
        @Test
        public void queryAllUser() throws IOException {
            String resource = "mybatis-config.xml";
    
            InputStream inputStream = Resources.getResourceAsStream(resource);
    
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            SqlSession sqlSession = sessionFactory.openSession();
    
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    
            List<User> users = mapper.queryAllUser();
    
            for (User user : users) {
                System.out.println(user);
            }
    
        }
    
    }
    

7.2、mybatis和spring的整合

7.2.1、方式一
  1. 创建spring的xml文件

  2. 编写数据源

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="url" value="jdbc:mysql://localhost:3306/commodity?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </bean>
    
  3. 编写 SqlSessionFactoryBean的注入数据源和管理mybatis.xml

    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="configLocation" value="classpath:mybatis-config.xml"/>
          <property name="mapperLocations" value="classpath:com/mhy/mapper/*.xml"/>
    </bean>
    
  4. 编写SqlSessionTemplate来获取sqlSession来获取Mapper操作数据库

        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <!--        使用构造器注入sessionFactory,因为没有set方法-->
            <constructor-arg index="0" ref="sessionFactory"/>
        </bean>
    
  5. 需要给Mapper接口加实现类并在没有spring中注册

    public class UserMapperImpl implements UserMapper {
        private SqlSessionTemplate sqlSessionTemplate;
        public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
            this.sqlSessionTemplate = sqlSessionTemplate;
        }
        
        @Override
        public List<User> queryAllUser() {
            return sqlSessionTemplate.getMapper(UserMapper.class).queryAllUser();
        }
    }
    
        <bean id="userMapper" class="com.mhy.mapper.UserMapperImpl">
            <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
        </bean>
    
7.2.2、方式二
  1. 前三步与方式一一样

  2. Impl类需要继承SqlSessionDaoSupport并实现Mapper接口

  3. 然后set注入sessionFactory就可以了(这里使用的注解Autowired和Qualifier自动装配的)

    @Repository(value = "userMapperImpl2")
    public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
    
        @Autowired
        @Qualifier(value = "sessionFactory")
        @Override
        public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
            super.setSqlSessionFactory(sqlSessionFactory);
        }
    
        @Override
        public List<User> queryAllUser() {
            return getSqlSession().getMapper(UserMapper.class).queryAllUser();
        }
    }
    

标签:userName,String,userCode,spring,phone,整合,address,mybatis,public
来源: https://www.cnblogs.com/shuisanya/p/16055792.html