maven项目spring整合mybatis
pom.xml
4.0.0org.exampleSSM1.0-SNAPSHOTorg.springframeworkspring-jdbc5.3.4mysqlmysql-connector-java8.0.20
org.mybatismybatis3.5.2org.mybatismybatis-spring2.0.6
com.alibabadruid1.1.12org.projectlomboklombok1.18.18junitjunit4.12test
src/main/resources**/*.properties**/*.xmltruesrc/main/java**/*.properties**/*.xmltrue
mybatis-config.xml
database.properties 数据库配置文件
jdbc.Url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
jdbc.username=root
jdbc.Password=123456
jdbc.maxActive=20
##### spring.xml
```xml
springJDBC.xml
spring-service.xml
Root实体类
package com.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//lombok
@AllArgsConstructor //全参构造方法
@NoArgsConstructor //无参构造
@Data //get set方法
public class Root {
private String user;
private String password;
}
dao层
IRootMapper接口
package com.dao;
import com.pojo.Root;
public interface IRootMapper {
public Root select(Root root);
}
IRootMapper.xml
select * from ssm.root where user =#{user} and password = #{password}
service层
IRootService service层接口
package com.service;import com.pojo.Root;public interface IRootService { public Root select(Root root);}
RootServiceImpl service层接口实现类
package com.service;import com.dao.IRootMapper;import com.pojo.Root;import lombok.Data;@Datapublic class RootServiceImpl implements IRootService{ private IRootMapper iRootMapper; @Override public Root select(Root root) { return this.iRootMapper.select(root); }}
测试
@org.junit.Testpublic void rootServiceTest(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springJDBC.xml","spring-service.xml"); RootServiceImpl impl = applicationContext.getBean("rootServiceImpl",RootServiceImpl.class); Root root = impl.select(new Root("root", "123456")); System.out.println(root);}