主要知识点: java的网络编程知识、mySql、mybatis
工具:eclipse、mySql、mySql界面工具Navicat
下面开始讲如何做出一个聊天机器人:
因为用到mybatis,需要导入俩个jar包:mybatis.jar、mysql-connector-java.jar,去maven仓库下载就可以了
我是用下面俩个的:
考虑兼容的问题不要用太高版本,之前用mysql-connector-java-8.0.12.jar包的时候,就报错了
首先,给你看下目录吧
配置mybatis
除了前面导入俩个jar包外,还需要创建conf.xml文件(里面是sql语句),mybatis.xml文件(数据库连接配置,指定conf.xml文件),DictionaryMapper.java(方法名需要跟conf的<select>的id一致,参数与<select>的parameterParam一致,返回值与<select>的ResultType的一致)
conf.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">
<mapper namespace="dictionary.DictionaryMapper">
<select id="findMessage" resultType="java.lang.String" parameterType="java.lang.String">
select response from dictionary where receive like #{receive}
</select>
</mapper>
mybatis.xml
数据库的url、username、password等信息对应自己电脑的。
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3307/android"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="resourses/conf.xml"/>
</mappers>
</configuration>
DictionaryMapper.java
package dictionary;
public interface DictionaryMapper {
String findMessage(String msg);
}
开发服务端
创建“RoberServer.java”文件,(名字有点调皮),代码如下,ServerSocket,定义端口号,ss.accept()是接收客户端发来的新的Socket,这里用到了mybaitis的使用。具体可以看下官网的详情:http://www.mybatis.org/mybatis-3/zh/getting-started.html
package dictionary;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class RoberServer {
public static void main(String[] args) {
try {
System.out.println("启动服务器");
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept(); //接收客户端发来的Socket
InputStream input = s.getInputStream();
OutputStream output = s.getOutputStream();
DataInputStream dataInput = new DataInputStream(input);
DataOutputStream dataOutput = new DataOutputStream(output);
String resource = "./resourses/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
DictionaryMapper mapper = session.getMapper(DictionaryMapper.class);
while(true) {
String readmsg = dataInput.readUTF();
System.out.println(readmsg);
//String msg = in.next();
String response = mapper.findMessage(readmsg);
dataOutput.writeUTF("机器人: "+response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
开发客户端
创建client.java
package dictionary;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
System.out.println("启动客户端");
Socket s = new Socket("127.0.0.1", 8888);
InputStream input = s.getInputStream();
OutputStream output = s.getOutputStream();
DataInputStream dataInput = new DataInputStream(input);
DataOutputStream dataOutput = new DataOutputStream(output);
Scanner in = new Scanner(System.in);
while(true) {
String msg = in.next();
dataOutput.writeUTF(msg);
String readmsg = dataInput.readUTF();
System.out.println(readmsg);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
数据库
创建数据库android,再创建表dictionary
CREATE TABLE `dictionary` (
`id` int(11) NOT NULL,
`receive` varchar(100) DEFAULT NULL,
`response` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
运行出来的结果:
需要启动RoberServer.java和Client.java