1.先完成Spring整合MyBatis技术
写一个Dept.java实体类 略
采用注解sql的方式写一个DeptMapper接口
public interface DeptMapper {
@Select("select * from DEPT")
public List<Dept> findAll();
}
配置applicationcontext.xml配置文件,配置数据流对象、加载 mybatis配置文件创建SqlSessionFactory、加载basePackage路径下的Mapper接口生成对象
<!-- 配置数据流对象 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:MLDN"></property>
<property name="username" value="scott"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 加载 mybatis配置文件创建SqlSessionFactory-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlmap-config.xml"></property>
</bean>
<!-- 加载basePackage路径下的Mapper接口生成对象 id名一般为接口名那首字母小写-->
<bean id="mapperscan" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="factory"></property>
<property name="basePackage" value="cn.xdl.mapper"></property>
</bean>
配置sqlmap-config.xml文件 日志、分页(jar包pagehelper)、加载EmpMapper映射器
<!-- 日志 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 分页 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
</plugins>
<!-- 加载EmpMapper映射器 -->
<mappers>
<mapper class="cn.xdl.mapper.DeptMapper" />
</mappers>
测试类。导jar包junit、spring-test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestDeptMapper {
@Autowired
private DeptMapper deptMapper;
@Test
public void test1(){
List<Dept> list = deptMapper.findAll();
for (Dept dept : list) {
System.out.println(dept.getDname()+" "+ dept.getLoc()+" "+dept.getDeptno());
}
}
}
接下来就是加入spring-mvc
加入一个Controller.java
package cn.xdl.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.xdl.entity.Dept;
import cn.xdl.mapper.DeptMapper;
@RestController//@Controller+ResponseBody
public class DeptController {
@Autowired
private DeptMapper deptMapper;
// @RequestMapping("/dept/list",method=RequestMethod.GET)
@GetMapping("/dept/list")
public List<Dept> loadAll(){
return deptMapper.findAll();
}
}
配置web.xml中的DispatcherServlet和handlermaping
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置applicationcontext.xml加上 找handlermaping、找controller的配置
<!-- 找handlermaping -->
<mvc:annotation-driven/>
<!-- 找controller -->
<context:component-scan base-package="cn.xdl"/>
测试运行到服务器上即可
项目所需要的jar包截图