Reputation: 25
I cannot get Mybatis's mapper by @Autowired
annotation. I use context-param
tag and contextConfigLocation
parameter to set all configuration which related to Spring framework, please refer to web.xml. I've tried something as below #1 and #2. I guest web.xml doesn't config my Spring xml file. If there is any idea, please help me to resolve it. Thanks~
It is a Maven project and it was created by eclipse, running Tomcat 9.0.8.
I've tried to use ApplicationContext
to get EmpMapper
, it works. Please refer to the code which I mark in Controller.
I also tried add EmpVO with a @Repository
, and use @Autowired
to get EmpVO instance in Controller, it works.
However, I cannot get instance of EmpMapper
by @Autowired
annotation.
Controller code:
package com.emp.controller;
@Controller
@RequestMapping("/emp")
public class EmpController {
@Autowired
EmpMapper empMapper;
@RequestMapping(value = "testInsert")
public void insertEmp() {
EmpVO empVO = new EmpVO();
empVO.setEname("RAYSUN2");
empVO.setComm(10000.0);
empVO.setHireDate(java.sql.Date.valueOf("2019-01-01"));
empVO.setDeptNo(20);
empVO.setSal(90000.0);
empMapper.insert(empVO);
//ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-mybatis.xml");
//EmpMapper empMapper = context.getBean(EmpMapper.class);
//empMapper.insert(empVO);
}
}
Mybatis's mapper interface:
package com.emp.mapper;
public interface EmpMapper {
void insert(EmpVO empVO);
void update(EmpVO empVO);
void delete(Integer empno);
EmpVO findByPrimaryKey(Integer empno);
List<EmpVO> getAll();
}
EmpMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.emp.mapper.EmpMapper">
<sql id="emp2_seq">emp2_seq.nextval</sql>
<resultMap type="com.emp.model.EmpVO" id="EmpVO">
<id property="empno" column="empno" jdbcType="DECIMAL" />
<result property="ename" column="ename" jdbcType="VARCHAR" />
<result property="job" column="job" jdbcType="VARCHAR" />
<result property="hireDate" column="hiredate" jdbcType="DATE" />
<result property="sal" column="sal" jdbcType="DOUBLE" />
<result property="comm" column="comm" jdbcType="DOUBLE" />
<result property="deptNo" column="deptno" jdbcType="DECIMAL" />
</resultMap>
<insert id="insert" parameterType="com.emp.model.EmpVO">
insert into emp2 (empno, ename, job, hiredate, sal, comm, deptno)
values
(<include refid="emp2_seq" />, #{ename}, #{job}, #{hireDate}, #{sal}, #{comm}, #{deptNo})
</insert>
</mapper>
spring-mybatis.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath*:/spring/model-config2-JndiObjectFactoryBean.xml"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml" />
<property name="mapperLocations" value="classpath*:/mybatis/mapper/**/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.emp.mapper" />
</bean>
</beans>
spring-ssm_webapp-mvc.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.emp">
</context:component-scan>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/spring/*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/springmvc/spring-ssm_webapp-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Upvotes: 1
Views: 4598
Reputation: 570
In your case, since you are using MyBatis, you could use @MapperScan and do something like this:
@MapperScan(basePackages={"com.emp.mapper"})
public abstract class MainSQLSessionFactoryConfiguration {
public abstract DataSource createDataSource();
@Bean(name = "txManager")
public PlatformTransactionManager txManager() {
return new DataSourceTransactionManager(createDataSource());
}
@Bean("sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(createDataSource());
sqlSessionFactoryBean.setMapperLocations(new ClassPathResource[] {
new ClassPathResource("rute/to/mapper/EmpMapper.xml")
});
sqlSessionFactoryBean.setTypeAliasesPackage("es.tendam.cep.domain;es.tendam.fwk.i18n.domain");
return sqlSessionFactoryBean.getObject();
}
}
Upvotes: 0
Reputation: 588
Please be aware of the @Mapper
annotation from mybatis, which allows seamless integration with spring framework so that you can inject the dependency of mapper anywhere using @Autowired
annotation. Please check the official documentation too. http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Upvotes: 1
Reputation: 25
Forget to add
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
it works after did it.
Upvotes: 0