默认的额mavenJDK版本为1.5在需要的时候需要对jdk进行升级
使用编译插件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
详情参见Mybaits逆向工程
db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/*?characterEncoding=utf-8
jdbc.username=root
jdbc.password=**
mybatis-config.xml:
<configuration>
configuration>
创建spring-dao.xml:在src/main/resources下new 一个Spring Bean Configuration File文件
需要选择命名空间context
spring-dao.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
beans>
配置扫描器,自动扫描bean(我使用的是注解)
<context:component-scan base-package="day0917.services">context:component-scan>
引入外部配置文件
<context:property-placeholder location="db.properties"/>
配置数据源,使用durid连接池
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="10" />
<property name="maxWait" value="10000" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<property name="defaultAutoCommit" value="true" />
bean>
sqlsessionfactory创建对象的配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
bean>
mapper扫描包的配置(mapper扫描器)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="day0917.dao" />
bean>
此处我自己写了一个statement在mapper里
<select id="selectList" resultMap="BaseResultMap" parameterType="day0917.pojo.SysRole">
select roleid, rolename, roledesc, rolestate from sysrole
<where>
<if test="rolestate!=null and rolestate!=-1">
and rolestate=#{rolestate}
if>
<if test="rolename!=null and rolename!=''">
and rolename like '%${rolename}%'
if>
where>
select>
//service接口
public interface SysRoleService {
/*
* 查询列表
* 方法名必须与mapper中statement的id值一致
* */
List<SysRole> selectList(SysRole role);
}
//采用注解的方式将实例类的bean放入ioc容器中
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService{
//采用自动装配的方式获取mapper的动态代理对象
@Autowired
private SysRoleMapper dao;
/*查询列表*/
public List<SysRole> selectList(SysRole role) {
return dao.selectList(role);
}
}
//测试类
public class Main1 {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-dao.xml");
SysRole role=new SysRole();
role.setRolename("员");
SysRoleService service=(SysRoleServiceImpl) ac.getBean("sysRoleService");
List<SysRole> list=service.selectList(role);
System.out.println(list);
}
}