配置pom.xml:
< properties>
< project.build.sourceEncoding> UTF-8</ project.build.sourceEncoding>
< maven.compiler.source> 1.8</ maven.compiler.source>
< maven.compiler.target> 1.8</ maven.compiler.target>
</ properties>
< dependencies>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.11</ version>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.9</ version>
</ dependency>
</ dependencies>
< build>
< resources>
< resource>
< directory> src/main/java</ directory>
< includes>
< include> **/*.properties</ include>
< include> **/*.xml</ include>
</ includes>
< filtering> false</ filtering>
</ resource>
</ resources>
</ build>
配置MyBatis-Config.xml(resources目录下):
< configuration>
< properties resource = " jdbc.properties" />
< environments default = " mysql" >
< environment id = " mysql" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " com/xxx/dao/xxxxxDao.xml" />
</ mappers>
</ configuration>
配置jdbc.properties(resources目录下):
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
配置utils.Myutil类:
public class MyBatisUtil {
private static SqlSessionFactory factory;
static {
try {
String config = "MyBatis-Config.xml" ;
InputStream is = Resources . getResourceAsStream ( config) ;
factory = new SqlSessionFactoryBuilder ( ) . build ( is) ;
} catch ( IOException e) {
factory = null ;
e. printStackTrace ( ) ;
}
}
public static SqlSession getSqlSession ( ) {
if ( factory != null ) {
return factory. openSession ( ) ;
}
return null ;
}
}
测试方法
public class test_01 {
@Test
public void test ( ) {
SqlSession sqlSession = MyBatisUtil . getSqlSession ( ) ;
assert sqlSession != null ;
StudentDao dao = sqlSession. getMapper ( StudentDao . class ) ;
List < Student > students = dao. selectStudents ( ) ;
for ( Student student : students) {
System . out. println ( student) ;
}
}
}
方法有多个参数
public List < Student > selectMultiParam ( @Param ( "studentName" ) String name, @Param ( "studentAge" ) int age) ;
mapper文件
< select id = " selectMultiParam" resultType = " com.atguigu.pojo.Student" >
select id,name,email,age from student where name = #{studentName} or age = #{studentAge}
</ select>
public List < Student > selectMultiObject ( QueryParam queryParam) ;
< select id = " selectMultiObject" resultType = " com.atguigu.pojo.Student" >
select id,name,email,age from student where name=#{queryName} or age = #{queryAge}
</ select>