1. Get the jar package, which is the maven method here. The specific contents are:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.10.RELEASE</version> </dependency>
Then edit the po info:
package com.duduli.li; public class Cp { private int id; private String chddm; private String bh; private String zh; private String sl; private String zt; private String gh; private String xssj; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getChddm() { return chddm; } public void setChddm(String chddm) { this.chddm = chddm; } public String getBh() { return bh; } public void setBh(String bh) { this.bh = bh; } public String getZh() { return zh; } public void setZh(String zh) { this.zh = zh; } public String getSl() { return sl; } public void setSl(String sl) { this.sl = sl; } public String getZt() { return zt; } public void setZt(String zt) { this.zt = zt; } public String getGh() { return gh; } public void setGh(String gh) { this.gh = gh; } /** * @return the xssj */ public String getXssj() { return xssj; } /** * @param xssj the xssj to set */ public void setXssj(String xssj) { this.xssj = xssj; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Cp [id=" + id + ", chddm=" + chddm + ", bh=" + bh + ", zh=" + zh + ", sl=" + sl + ", zt=" + zt + ", gh=" + gh + ", xssj=" + xssj + "]"; } }
The following is the configuration of spring:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/hnfc?serverTimezone=UTC" /> <property name="username" value="root" /> <property name="password" value="xxxxxx" /> <!-- Connection initial value, the default value of the initial value of the number of connections created when the connection pool starts is 0 --> <property name="initialSize" value="3" /> <!-- Minimum idle value. When the number of idle connections is less than the threshold, the connection pool will pre-apply for some connections, so as not to apply for too late when the flood peaks. The default value is 0 --> <property name="minIdle" value="3" /> <!-- The maximum idle value. After a peak time, the connection pool can slowly release some of the unused connections until it is reduced to maxIdle. The default value of 0 is 8 --> <property name="maxIdle" value="5" /> <!-- The maximum value of the connection pool, the maximum number of connections that can be allocated from the pool at the same time, when 0 is unlimited, the default value is 8 --> <property name="maxTotal" value="15" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <!-- This configuration is to solve the mapping of mapper interface when spring is running --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.duduli.li" /> </bean> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.duduli.li"></context:component-scan> </beans>
Note here
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.duduli.li" /> </bean>
Next the interface of mybatis
@Repository("mapper") public interface CPmapper { @Select("select * from cp") public List<Cp> getAllCP(); @Select("select id from cp") public List<Integer> getAllId(); }
Next, the method interface
public interface CPservice { public List<Cp> getAllCP(); public void getAllId(); }
Method implementation class:
@Component("cpservice") public class CPserviceImpl implements CPservice { @Autowired @Qualifier("mapper") private CPmapper cpmapper; public List<Cp> getAllCP() { // TODO Auto-generated method stub for(Cp c:cpmapper.getAllCP()) { System.out.println(c.toString()); } return cpmapper.getAllCP(); } public void getAllId() { for(int i:cpmapper.getAllId()) { System.out.println(i); } } }
And finally the test class:
public static void main(String[] args) { // TODO Auto-generated method stub String[] files = {"com/duduli/li/springContext.xml" }; ApplicationContext context = new ClassPathXmlApplicationContext(files); // UserService userService = (UserService) context.getBean("userService"); CPservice cpservice = (CPservice) context.getBean("cpservice"); // cpservice.getAllCP(); cpservice.getAllId(); }