方式清单
(先将jar包导完后)
-
方式一
思路
- 将数据库连接有关的信息配置到
db.properties
里 - 为了方便跟进程序,在
mybatis-config.xml
中将mybatis的自带日志打开 - 在
applicationContext.xml
中配置数据源、sqlSessionFactory、自动扫描对象关系映射等 - 建立包:entities、dao、mapper、service、service.impl,并创建相应类或接口
代码
整体目录结构:
最终目录结构 - 将数据库连接有关的信息配置到
配置db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/xzl?useUnicode=true&characterEncoding=utf8
jdbc.username=aaa
jdbc.password=123
配置mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>
配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 导入外部配置文件 -->
<context:property-placeholder location="db.properties"/>
<!-- 配置service的实现类 -->
<bean name="qqun_ListService" class="demo.cyj.service.impl.Qqun_ListServiceImpl"></bean>
<!-- 数据源配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- sqlSessionFactory给spring托管 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 别名,类名开头变小写 -->
<property name="typeAliasesPackage" value="demo.cyj.entities"></property>
<!-- sql映射文件路径 -->
<!-- classpath *和不加*的区别 -->
<property name="mapperLocations" value="classpath*:demo/cyj/mapper/*Mapper.xml"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!--自动扫描对象关系映射 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 指定要自动扫描接口的基础包,实现接口 -->
<property name="basePackage" value="demo.cyj.dao"></property>
</bean>
</beans>
实体类Q群列表:Qqun_List.java
package demo.cyj.entities;
import java.io.Serializable;
public class Qqun_List implements Serializable{
private static final long serialVersionUID = -8969192680204852279L;
private String groupId;
private String groupName;
private String auth;
private String flag;
public Qqun_List() {}
public Qqun_List(String groupId, String groupName, String auth, String flag) {
super();
this.groupId = groupId;
this.groupName = groupName;
this.auth = auth;
this.flag = flag;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getAuth() {
return auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@Override
public String toString() {
return "Qqun_List [groupId=" + groupId + ", groupName=" + groupName + ", auth=" + auth + ", flag=" + flag + "]";
}
}
dao接口Qqun_ListDao.java
package demo.cyj.dao;
import java.util.List;
import demo.cyj.entities.Qqun_List;
public interface Qqun_ListDao {
public List<Qqun_List> findAll();
}
配置mapper文件Qqun_ListMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--此处的namespace需要和 接口的路径绑定 -->
<mapper namespace="demo.cyj.dao.Qqun_ListDao">
<select id="findAll" resultType="qqun_List">
select * from qqun_list
</select>
</mapper>
service接口Qqun_ListService.java
package demo.cyj.service;
import java.util.List;
import demo.cyj.entities.Qqun_List;
public interface Qqun_ListService {
public List<Qqun_List> findAll();
}
service接口实现类Qqun_ListServiceImpl.java
package demo.cyj.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import demo.cyj.dao.Qqun_ListDao;
import demo.cyj.entities.Qqun_List;
import demo.cyj.service.Qqun_ListService;
public class Qqun_ListServiceImpl implements Qqun_ListService{
@Autowired
private Qqun_ListDao qqun_ListDao;
@Override
public List<Qqun_List> findAll() {
// TODO Auto-generated method stub
return qqun_ListDao.findAll();
}
}
编写测试类Test1.java
package demo.cyj;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.cyj.entities.Qqun_List;
import demo.cyj.service.Qqun_ListService;
public class Test1 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Qqun_ListService qqun_ListService = ac.getBean(Qqun_ListService.class);//单例才能这样传类类型
List<Qqun_List> list = qqun_ListService.findAll();
System.out.println(list);
}
}
运行结果
后面的明天写了
-
方式二
-
方式三
-
方式四
网友评论